Asp.net mvc 在MVC5中以部分视图提交表单时,模型为空
我只是在尝试学习MVC,并且面临一些问题。当我提交我的部分视图时,我在ModelBloginside Create Method中得到null 我做错了什么,正确的方法是什么 查看(Index.cshtml)Asp.net mvc 在MVC5中以部分视图提交表单时,模型为空,asp.net-mvc,asp.net-mvc-4,razor,asp.net-mvc-5,model-binding,Asp.net Mvc,Asp.net Mvc 4,Razor,Asp.net Mvc 5,Model Binding,我只是在尝试学习MVC,并且面临一些问题。当我提交我的部分视图时,我在ModelBloginside Create Method中得到null 我做错了什么,正确的方法是什么 查看(Index.cshtml) @model IEnumerable @{ ViewBag.Title=“Index”; } 指数 @ActionLink(“新建”、“创建”) 样品 统一资源定位地址 名称 @foreach(模型中的var项目){ @ActionLink(“编辑”,“编辑”,新的{/*id=item
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
样品
统一资源定位地址
名称
@foreach(模型中的var项目){
@ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/})
@item.URL
@项目名称
}
@Html.Partial(“\u CreateBlog”,new Samples.Controllers.Blog())
部分视图(\u CreateBlog.cshtml)
@model Samples.Controllers.Blog
@使用(Html.BeginForm(“Create”、“Sample”、FormMethod.Post))
{
@Html.AntiForgeryToken()
博客
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.URL)
@EditorFor(model=>model.URL)
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
}
@ActionLink(“返回列表”、“索引”)
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;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
名称空间示例.控制器
{
公共类SampleController:控制器
{
列出你的博客;
公共采样控制器()
{
lstBlogs=新列表
{
新博客{Name=“Domnic”,URL=“www.google.com”},
新博客{Name=“Tom”,URL=“www.YAHOO.com”},
新博客{Name=“Cat”,URL=“www.facebook.com”},
新博客{Name=“Bob”,URL=“www.twitter.com”}
};
}
//获取:样本
公共行动结果索引()
{
返回视图(lstblog);
}
公共行动结果索引WithDynamicView()
{
返回视图(lstblog);
}
[HttpPost]
公共void创建(博客)
{
}
}
公共类博客
{
公共字符串名称;
公共字符串URL;
}
}
您的类博客
只包含字段,而不包含属性,因此DefaultModelBinder
无法设置其值。将其更改为添加getter/setter
public class Blog
{
public string Name { get; set; }
public string URL { get; set; }
}
您的类
Blog
只包含字段,而不包含属性,因此DefaultModelBinder
无法设置其值。将其更改为添加getter/setter
public class Blog
{
public string Name { get; set; }
public string URL { get; set; }
}
也许这个答案与你的问题有关:
请注意。这就解释了一切。也许这个答案与您的问题有关: 请注意。这就解释了一切
public class Blog
{
public string Name { get; set; }
public string URL { get; set; }
}