2016-08-17 3 views
-1

ニュースをデータベースに追加します。選択したカテゴリにこのコードを使用すると返信DropDownListを使用するとnullが戻される

Me Null。どうすれば解決できますか?

<select name="category"> 
    @{ 
     foreach (var item in RCat.MainCategory().Where(a=>a.ParentID==1)) 
     { 
      <option value="@item.ID">@item.Title</option> 
     } 
    } 

</select> 

マイコントローラー:

[HttpPost] 
[ValidateInput(false)] 
public ActionResult CreateNews(Tbl_News t, HttpPostedFileBase pic, int Category) 
{ 
    try 
    { 
     if (Session["Username"] == null) 
      return RedirectToAction("Register", "Home"); 
     if (pic == null) 
     { 
      ViewBag.Style = "color:red"; 
      ViewBag.Message = "خطایی رخ داده . باید عکسی را برای خبر انتخاب کنید"; 
      return View(); 
     } 
     if (Category == 0) 
     { 
      ViewBag.Style = "color:red"; 
      ViewBag.Message = "خطایی رخ داده . باید یکی از دسته ها را انتخاب کنید"; 
      return View(); 
     } 
     t.Date = DateTime.Now.Date; 
     t.Dislike = 0; 
     t.Like = 0; 
     t.Visit = 0; 
     t.Username = Session["Username"].ToString(); 
     if (t.Type != "iran" && t.Type != "world" && t.Type != "sport" && t.Type != "art" && t.Type != "knowledge" && t.Type != "economic" && t.Type != "memo" && t.Type != "you" && t.Type != "pic" && t.Type != "video") 
      t.Type = null; 


     if (ModelState.IsValid) 
     { 
      if (pic != null) 
      { 
       if (pic.ContentLength <= 512000) 
       { 
        if (pic.ContentType == "image/jpeg") 
        { 
         Random rnd = new Random(); 
         string picname = rnd.Next().ToString(); 
         string path = System.IO.Path.Combine(Server.MapPath("~/Content/img/NewsPic/")); 
         pic.SaveAs(path + picname); 
         t.Image = picname; 
         db.Tbl_News.Add(t); 
         if (Convert.ToBoolean(db.SaveChanges())) 
         { 
          var q = (from a in db.Tbl_News 
            orderby a.Date descending 
            select a).FirstOrDefault(); 
          Tbl_Cat_News TCN = new Tbl_Cat_News(); 
          TCN.NewsID = q.ID; 
          TCN.CatID = Category; 
          db.Tbl_Cat_News.Add(TCN); 
          if (Convert.ToBoolean(db.SaveChanges())) 
          { 
           ViewBag.Style = "color:green"; 
           ViewBag.Message = "با موفقیت ثبت شد"; 
           return View(); 
          } 
          else 
          { 
           ViewBag.Style = "color:red"; 
           ViewBag.Message = "خطایی رخ داده . خبر ثبت نشد"; 
           return View(); 
          } 
         } 
         else 
         { 
          ViewBag.Style = "color:red"; 
          ViewBag.Message = "خطایی رخ داده . خبر ثبت نشد"; 
          return View(); 
         } 
        } 
        else 
        { 
         ViewBag.Style = "color:red"; 
         ViewBag.Message = "خطایی رخ داده . فرمت تصویر باید jpg باشد"; 
         return View(); 
        } 

       } 
       else 
       { 
        ViewBag.Style = "color:red"; 
        ViewBag.Message = "خطایی رخ داده . حجم تصویر باید بیشتر کمتر از 515 کیلو بایت باشد"; 
        return View(); 
       } 
      } 

      else 
      { 
       return RedirectToAction("Logout", "Admin"); 
      } 
     } 
     else 
     { 
      ViewBag.Style = "color:red"; 
      ViewBag.Message = "خطایی رخ داده . تمامی فیلدها باید پر شوند"; 
      return View(); 
     } 
    } 
    catch (Exception) 
    { 

     return Content("خطا"); 
    } 
} 
+0

あなたのビューでは、何かを 'Type'にバインドしていますか? (あなたが表示しているのは、 'name =" Category "' ' –

+0

no。をバインドしていない' (それほどひどいループではない)を生成する –

答えて

0

あなたはどのようなタイプは、で任意の条件をと一致していませので、もしそれが発射し、すべてのOR条件を持っている "t.Type = NULL;"そのため、あなたは常に "NULL"値を取得します。

関連する問題