mvc 4 模型为空
Posted
技术标签:
【中文标题】mvc 4 模型为空【英文标题】:mvc 4 Model is null 【发布时间】:2013-08-27 18:23:38 【问题描述】:我是第一次使用 razor 的 listboxfor,但我的 Model 始终为空。 在阅读了类似的帖子和试用后,它仍然无法正常工作。
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 PersonViewModelName = "Test", Surrname="testsurrname", Age=30);
var viewModel = new PersonViewModel()
ListboxData = data.AsEnumerable().Select(s=> new SelectListItemValue=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;
将值从 editFor 写入 db 按预期工作,无需 listboxfor 代码。 将它添加到我的 html 后,它应该在页面加载时从 db 填充,但在页面加载时我得到一个 ReferenceNotSet 异常。在调用 GetListBoxData 操作之前,Model.ListboxData 为 null。
非常感谢您的帮助!
【问题讨论】:
为什么你的视图文件中模型声明后有一个“”?
不好意思,之前有一些html代码,从cshtml复制粘贴 Person.cshtml?您的控制器是 Index.... 您有 Person 操作吗?因为默认情况下它会通过它。通常你会有 index.cshtml 【参考方案1】:-
您的表单应该通过 POST 而不是 GET 提交数据。而且,您不需要使用
enctype = "multipart/form-data"
,除非您想通过您的 from 上传文件。
您的控制器中需要两个索引操作,一个用于将数据从控制器发送到视图,另一个用于在表单提交 (POST) 到服务器时从视图中获取数据.
传递给 ListBox 的第一个参数(表达式)是指模型中的 Property,ListBox 中的选定项将存储在其中,在本例中为 PersonId。
所以,你的视图应该是这样的:
@model MVCApplication.Web.ViewModel.PersonViewModel
@using (Html.BeginForm("Index", "Person"))
@Html.ListBoxFor(model => model.PersonId, Model.ListBoxData)
<input type="submit" value="Save" />
然后,在您的 Controller 中,您将拥有两个像这样的 Action:
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;
【讨论】:
感谢您的澄清,但问题仍然存在(模型为空)。我的 Html 中有一些 EditorFor 元素,它们与控制器 -> 数据库中的创建方法连接。这工作正常。在控制器索引方法中调试不起作用,它在初始化页面之前崩溃,模型为空。是否必须在某处显式设置以进行空检查?感谢您的宝贵时间 崩溃是什么意思? 它在我的 cshtml 中停止,引用未设置异常(因为模型为空) 您的操作与我回答中的操作完全一样吗?您是否在 Action 中从数据库中获取了正确的数据? Model 为 null 与 ListBox 没有任何关系。以上是关于mvc 4 模型为空的主要内容,如果未能解决你的问题,请参考以下文章