gpt4 book ai didi

c# - 模型为空?

转载 作者:行者123 更新时间:2023-11-30 22:42:27 29 4
gpt4 key购买 nike

我读过很多关于 null 模型的类似帖子,但我的案例非常非常简单,Create 操作上的模型仍然是 null。我究竟做错了什么???

情况是这样的:一个主视图,内部有两个强类型的部分 View ,每个 View 都绑定(bind)到主模型的一个公共(public)属性。任何帮助表示赞赏。模型:

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; }
}

Controller :

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 个强大的局部 View ——每个 View 都用于模型

<%@ 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>
<% } %>

最佳答案

我要大胆地说,在将 ComplexModel 发回服务器时,您的模型验证失败

您的模型验证根本不必失败。您没有从 if block 中返回任何内容,因此您总是返回一个没有关联模型的 View :

if (ModelState.IsValid)
{
var test = model.SomeStringsComplex;
}
return View(); // View is called with no Model data

根据您的代码判断,这会导致 Create View 在没有模型的情况下被实例化。这可以很简单地解决:

[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);
}

关于c# - 模型为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4410833/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com