2014-04-15 75 views
1

每次此表單都會回發我在我的控制器中將viewmodel視爲null。任何人都可以請幫我,我在這裏失蹤了什麼?無法將模型從視圖傳遞給控制器​​在FormSubmit上

這裏是我的ViewModel

public class CarViewModel 
{ 
    [Required(ErrorMessage = "Please enter the Reg No")] 
    public string RegNo { get; set; } 

    [Required(ErrorMessage = "Please enter Model")] 
    public string CarModel { get; set; } 

    [Required(ErrorMessage = "Please select one of the status")] 
    public int? Status { get; set; } 

    [Required(ErrorMessage = "Please enter colour")] 
    public string Colour { get; set; } 

    public List<SelectListItem> ModelList 
    { 
     get; 
     set; 
    } 

    public List<SelectListItem> Statuses 
    { 
     get; 
     set;   
    } 
} 

這裏是我的控制器

[HttpGet] 
    public ActionResult Create() 
    { 
     CarViewModel model = new CarViewModel(); 
     var statusList = _service.GetAllStatusList(); 
     var modelList = _service.GetAllModelList(); 
     model.Statuses = statusList.Select(x => new SelectListItem 
     { 
      Text = x.description, 
      Value = x.id.ToString() 
     }).ToList(); 

     model.ModelList = modelList.Select(x => new SelectListItem 
     { 
      Text = x.model_name, 
      Value = x.model_id.ToString() 
     }).ToList(); 

     return View(model); 
    } 

    // 
    // POST: /CarRental/Create 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(CarViewModel model) 
    { 
     try 
     { 
      // TODO: Add insert logic here 
      if (ModelState.IsValid) 
      { 
       car car = new car { colour = model.Colour, registration = model.RegNo, status = model.Status }; 
       _service.RegisterCar(car); 
       return View(model); 
      } 
      else 
      { 
       return View(model); 
      } 
      // return RedirectToAction("Index"); 
     } 
     catch (Exception) 
     { 
      return View(model); 
     } 
    } 

這是我的看法

@model CarRentalUI.Models.CarViewModel 
    @{ 
    ViewBag.Title = "Create"; 
    } 

    <h2>Create</h2> 

    @using (Html.BeginForm("Create","CarRental",FormMethod.Post)) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     @Html.LabelFor(x=>x.RegNo) 
     @Html.TextBoxFor(x=>x.RegNo)   
     @Html.ValidationMessageFor(x=>x.RegNo) 
     <br/> 
     @Html.LabelFor(x=>x.CarModel)   
     @Html.DropDownListFor(x=>x.CarModel,Model.ModelList,"Please Make a Selection") 
     @Html.ValidationMessageFor(x=>x.Model) 

     <br/> 
     @Html.LabelFor(x=>x.Status)   
     @Html.DropDownListFor(x=>x.Status, Model.Statuses,"Please Make a Selection")  
     @Html.ValidationMessageFor(x=>x.Status) 

     <input type="submit" value="Submit" id="submitButton"/> 
    } 
+1

爲什麼你需要嗨丹頓控制在這裏? –

+0

這是其他文章中提到的解決方案之一,所以我嘗試了與它的運氣,但仍然沒有工作。 – Sike12

+1

請嘗試使用html.editorfor而不是html.textboxfor,然後參見 –

回答

1
  • 取出HiddenFor的
  • 確保狀態的值是可解析INT的
  • 添加顏色下拉

我只是根據創建了一個測試項目中的示例代碼,它似乎工作(http://i.imgur.com/PIFACMd.png

+0

簡單圖像如何幫助解決OP問題?如果您在答案中添加一些代碼會更好。 – Leron

+0

@Leron,這是他的代碼... – matthijsb

+0

對不起,我沒有明白這一點。 – Leron

1

使用

return RedirectToAction("Create"); 

而不是

return View(model); 
+0

這應該沒有什麼區別,MVC會自動將Action名稱作爲視圖名稱。除此之外,加載視圖的作品,他只是得到一個空的模型 – matthijsb

+0

使用返回RedirectToAction(「創建」); – DMishra

+0

這也不是問題... – matthijsb

相關問題