在表单提交时无法将模型从View传递给Controller

时间:2014-04-15 12:09:02

标签: c# asp.net .net asp.net-mvc asp.net-mvc-5

每次这个表单发回一个帖子我都会在我的控制器中将viewmodel视为null。谁能帮助我,我在这里失踪了什么?

这是我的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"/>
  }

2 个答案:

答案 0 :(得分:1)

使用

return RedirectToAction("Create");

而不是

 return View(model);

答案 1 :(得分:1)

  • 删除HiddenFor&#39;
  • 确保状态值可解析为
  • 添加颜色下拉列表

我刚刚根据您的示例代码创建了一个测试项目,它似乎有效(http://i.imgur.com/PIFACMd.png