我已经阅读了很多关于null模型的类似帖子,但我的情况非常简单,而且仍然是Create action上的模型为null。我究竟做错了什么???
以下是这种情况:一个主视图,内部有两个强类型的局部视图,每个视图都绑定到主模型的公共属性。任何帮助表示赞赏。 的模型:
public class SimpleModel1
{
public IEnumerable<string> SomeStrings1 { get; set; }
}
public class SimpleModel2
{
public IEnumerable<string> SomeStrings2 { get; set; }
}
public class ComplexModel
{
public SimpleModel1 model1 { get; set; }
public SimpleModel2 model2 { get; set; }
public IEnumerable<string> SomeStringsComplex { get; set; }
}
他控制器:
public ActionResult Create()
{
ComplexModel complex = new ComplexModel();
complex.model1 = new SimpleModel1();
complex.model1.SomeStrings1 = new List<string> { "a1", "a2", "a3"};
complex.model2 = new SimpleModel2();
complex.model2.SomeStrings2 = new List<string> { "b1", "b2", "b3" };
complex.SomeStringsComplex = new List<string> { "c1", "c2", "c3" };
return View(complex);
}
[HttpPost]
public ActionResult Create(ComplexModel model)
{
if (ModelState.IsValid)
{
var test = model.SomeStringsComplex;
}
return View();
}
视图: 2个强烈的局部视图 - 每个模型
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<MvcApp1.Models.SimpleModel2>" %>
<fieldset>
<legend>Fields</legend>
<% foreach (string item in Model.SomeStrings2)
{%>
<p>
<label for="Title">Item Title:</label>
<%= Html.TextBox(item,item)%>
</p>
<%
}
%>
</fieldset>
1主要观点:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcApp1.Models.ComplexModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<% using (Html.BeginForm()) {%>
<fieldset>
<div> Own values
<% foreach (string item in Model.SomeStringsComplex)
{%>
<p>
<label for="Title">Item Title:</label>
<%= Html.TextBox(item,item) %>
</p>
<%
}
%>
</div>
<div>Simple values 1
<%Html.RenderPartial("SimpleModelView1", this.ViewData.Model.model1, new ViewDataDictionary()); %>
</div>
<div>Simple values 2
<%Html.RenderPartial("SimpleModelView2", Model.model2, new ViewDataDictionary()); %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
答案 0 :(得分:2)
我将采取疯狂的刺,并说当将ComplexModel发布回服务器时,模型验证失败
您的模型验证根本不必失败。您没有从if
块内返回任何内容,因此您总是返回一个没有关联模型的视图:
if (ModelState.IsValid)
{
var test = model.SomeStringsComplex;
}
return View(); // View is called with no Model data
根据您的代码判断,这将导致Create视图在没有Model的情况下实例化。这可以很简单地解决:
[HttpPost]
public ActionResult Create(ComplexModel model)
{
if (ModelState.IsValid)
{
var test = model.SomeStringsComplex;
// Do something to Create the object
RedirectToAction("Index");
}
// Model State is invalid, return so the user can correct
return View(model);
}
答案 1 :(得分:1)
你不应该在那条线上向视图发送内容吗?
return View();
答案 2 :(得分:0)
我认为问题在于如何指定文本字段 - 模型绑定器不知道如何将它们分配回它们来自的属性。
您可能希望阅读这篇Haacked文章,该文章介绍了如何对可枚举属性使用模型绑定:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
答案 3 :(得分:0)
最后我找到了解决方案。发布到Create方法的数据应该是s.th.像这样:
[HttpPost]
public ActionResult Create(ComplexModel mainModel, SimpleModel nestedModel, SimpleModel2 nested2Model)
{
if (ModelState.IsValid)
{
var main = mainModel;
var nested1 = nestedModel;
var nested2 = nested2Model;
}
return View();
}
当模型绑定到部分视图时,嵌套参数不为null。如果通过主模型在主窗体中使用,则值在主模型的实例中。我还将IEnumerable<string>
更改为IList<string>
,然后使用
for (int i = 0; i < Model.Strings.Count; i++)
{ %>
<%: Html.EditorFor(m => m.Strings[i])%>
<% }
全部谢谢