在 MVC5 中以局部视图提交表单时模型为空
Model is null while submitting the form in partial view in MVC5
我只是想学习 MVC 并面对一些问题 issues.When 我正在提交我的局部视图,我在模型 博客 中的创建方法中得到 null。
我做错了什么,正确的做法是什么?
查看(Index.cshtml)
@model IEnumerable<Samples.Controllers.Blog>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Sample</th>
<th>URL</th>
<th>Name</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
<td>
@item.URL
</td>
<td>
@item.Name
</td>
</tr>
}
</table>
@Html.Partial("_CreateBlog", new Samples.Controllers.Blog())
局部视图(_CreateBlog.cshtml)
@model Samples.Controllers.Blog
@using (Html.BeginForm("Create","Sample",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Blog</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
@Html.LabelFor(model => model.URL)
@Html.EditorFor(model => model.URL)
</div>
<div class="row">
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
SampleController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Samples.Controllers
{
public class SampleController : Controller
{
List<Blog> lstBlogs;
public SampleController()
{
lstBlogs = new List<Blog>
{
new Blog{ Name="Domnic", URL= "www.google.com"},
new Blog{ Name="Tom", URL= "www.YAHOO.com"},
new Blog{ Name="Cat", URL= "www.facebook.com"},
new Blog{ Name="Bob", URL= "www.twitter.com"}
};
}
// GET: Sample
public ActionResult Index()
{
return View(lstBlogs);
}
public ActionResult IndexWithDynamicView()
{
return View(lstBlogs);
}
[HttpPost]
public void Create(Blog blog)
{
}
}
public class Blog
{
public string Name;
public string URL;
}
}
您的 class Blog
仅包含字段,不包含属性,因此 DefaultModelBinder
无法 设置 它们的值。更改它添加 getters/setters
public class Blog
{
public string Name { get; set; }
public string URL { get; set; }
}
也许这个答案与您的问题有关:Is there a reason why the default modelbinder doesn't bind to fields?
关注DefaultModelBinder, ModelBinderContext, ModelMetadata。这说明了一切。
我只是想学习 MVC 并面对一些问题 issues.When 我正在提交我的局部视图,我在模型 博客 中的创建方法中得到 null。
我做错了什么,正确的做法是什么?
查看(Index.cshtml)
@model IEnumerable<Samples.Controllers.Blog>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Sample</th>
<th>URL</th>
<th>Name</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
<td>
@item.URL
</td>
<td>
@item.Name
</td>
</tr>
}
</table>
@Html.Partial("_CreateBlog", new Samples.Controllers.Blog())
局部视图(_CreateBlog.cshtml)
@model Samples.Controllers.Blog
@using (Html.BeginForm("Create","Sample",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Blog</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
@Html.LabelFor(model => model.URL)
@Html.EditorFor(model => model.URL)
</div>
<div class="row">
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
SampleController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Samples.Controllers
{
public class SampleController : Controller
{
List<Blog> lstBlogs;
public SampleController()
{
lstBlogs = new List<Blog>
{
new Blog{ Name="Domnic", URL= "www.google.com"},
new Blog{ Name="Tom", URL= "www.YAHOO.com"},
new Blog{ Name="Cat", URL= "www.facebook.com"},
new Blog{ Name="Bob", URL= "www.twitter.com"}
};
}
// GET: Sample
public ActionResult Index()
{
return View(lstBlogs);
}
public ActionResult IndexWithDynamicView()
{
return View(lstBlogs);
}
[HttpPost]
public void Create(Blog blog)
{
}
}
public class Blog
{
public string Name;
public string URL;
}
}
您的 class Blog
仅包含字段,不包含属性,因此 DefaultModelBinder
无法 设置 它们的值。更改它添加 getters/setters
public class Blog
{
public string Name { get; set; }
public string URL { get; set; }
}
也许这个答案与您的问题有关:Is there a reason why the default modelbinder doesn't bind to fields?
关注DefaultModelBinder, ModelBinderContext, ModelMetadata。这说明了一切。