mvc 4模型为空
本文关键字:模型 mvc | 更新日期: 2023-09-27 18:28:59
我第一次使用razor的listboxfor,但我的Model始终为null。在阅读了类似的帖子并试用后,它仍然不起作用。
Person.cshtml
@model SampleApp.Web.ViewModel.PersonViewModel
@{
ViewBag.Title = "Welcome";
}
<article>
<p>
Welcome to example page.
</p>
<p>
<div class="container">
//Post data works as expected, controllers create method write to db successfully
@using (Html.BeginForm("Create", "Person", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Personen</legend>
<div class="editor-label">
@* @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Surrname)
</div>
</fielset>
</div>
<p>
<input type="submit" value="Create" />
</p>
}
//binding to Model fails, Model is null. Not be able to debug anything in controller action, it stops when "loading" the page
@using (Html.BeginForm("GetListBoxData", "Person"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.ListBoxFor(model => model.ListboxData, Model.ListboxData);
}
</div>
PersonController.cs
[AcceptVerbs(HttpVerbs.Get)]
[ValidateAntiForgeryToken]
public ActionResult GetListBoxData()
{
var data = new List<PersonViewModel>();
data.Add(new PersonViewModel{Name = "Test", Surrname="testsurrname", Age=30});
var viewModel = new PersonViewModel()
{
ListboxData = data.AsEnumerable().Select(s=> new SelectListItem{Value=s.Name ,Text = s.Surrname}),
};
return View(viewModel);
}
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult GetListBoxData(PersonViewModel persondata)
{
//TODO: handle values from View
return View(this);
}
[ValidateAntiForgeryToken]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include = "Name, Surrname, Age")] PersonViewModel persondata)
{
try
{
PersonService personDataProvider = new PersonService();
personDataProvider.SavePerson(persondata);
return new RedirectResult("SomewhereToGo");
}
catch (DataException ex)
{
//TODO: Log
}
return View(this);
}
PersonViewModel
public class PersonViewModel
{
public int PersonId{ get; set; }
public int Age { get; set; }
public string Name { get; set; }
public string Surrname { get; set; }
public IEnumerable<SelectListItem> ListboxData { get; set; }
}
在没有listboxfor代码的情况下,将值从editFor写入db可以正常工作。在将它添加到我的html后,应该在页面加载时从db中填充它,但在页面加载中我得到了ReferenceNotSet异常。在调用GetListBoxData操作之前,Model.ListboxData为null。
非常感谢你的帮助!
- 您的表单应该通过POST而不是GET提交数据。而且,您不需要使用
enctype = "multipart/form-data"
,除非您想通过from上传文件 - 在控制器中需要两个索引操作,一个用于将数据从控制器发送到视图,另一个用于在表单提交(POST)到服务器时从视图取回数据
- 传递给ListBox(表达式)的第一个参数指的是模型中的属性,ListBox中选定的项将存储在该属性中,在本例中为PersonId
所以,你的视图应该是这样的:
@model MVCApplication.Web.ViewModel.PersonViewModel
@using (Html.BeginForm("Index", "Person"))
{
@Html.ListBoxFor(model => model.PersonId, Model.ListBoxData)
<input type="submit" value="Save" />
}
然后,在您的控制器中,您将有两个类似的操作:
public ActionResult Index()
{
var viewModel = new PersonViewModel()
{
ListboxData = data.Select(s => new SelectListItem { Value = s.PersonId.ToString(), Text = s.PersonId.ToString() }).AsEnumerable();
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(PersonViewModel viewModel)
{
// code to save the data in the database or whatever you want to do with the data coming from the View
}
顺便说一句,在ViewModel中,您不必像那样定义ListBoxData属性,只需执行以下操作:
public class PersonViewModel
{
public int PersonId{ get; set; }
public IEnumerable<SelectListItem> ListBoxData { get; set; }
}