asp.net-mvc-3 – MVC3 – 使用ViewModel插入 – 对象引用未设置
发布时间:2020-12-15 23:45:18 所属栏目:asp.Net 来源:网络整理
导读:我有两个模型,如下所示,并尝试从一个视图将其中一个插入数据库.我创建了一个视图模型来试图这样做. public class Blog{ public int BlogID { get; set; } public string Title { get; set; } public DateTime CreatedOn { get; set; } public virtual User U
我有两个模型,如下所示,并尝试从一个视图将其中一个插入数据库.我创建了一个视图模型来试图这样做.
public class Blog { public int BlogID { get; set; } public string Title { get; set; } public DateTime CreatedOn { get; set; } public virtual User User { get; set; } public virtual ICollection<BlogPost> Posts { get; set; } } public class BlogPost { public int PostID { get; set; } public string Body { get; set; } public DateTime CreatedOn { get; set; } public int UserID { get; set; } public virtual User User { get; set; } } public class BlogViewModel { public Blog Blog { get; set; } public BlogPost BlogPost { get; set; } } 使用视图模型我发布到创建控制器: [HttpPost] public ActionResult Create(BlogViewModel blog) { if (ModelState.IsValid) { User user = unit.UserRepository.GetUser(); blog.Blog.User = user; blog.Blog.CreatedOn = DateTime.Now; unit.BlogRepository.Insert(blog.Blog); unit.BlogPostRepository.Insert(blog.BlogPost); unit.Save(); return RedirectToAction("Index"); } return View(blog); } 这不断抛出错误
在线上blog.Blog.User =用户. 对我做错了什么的想法? 编辑 public ActionResult Create(Blog Blog,BlogPost post) 然后它一切工作.那么为什么不以viewmodel格式发送数据?我使用基于视图模型的视图和控制器之间的交互 @model Test.Models.BlogViewModel @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Blog</legend> <div class="editor-label"> @Html.LabelFor(model => model.Blog.Title) </div> <div class="editor-field"> @Html.EditorFor(model => model.Blog.Title) @Html.ValidationMessageFor(model => model.Blog.Title) </div> <div class="editor-label"> @Html.LabelFor(model => model.BlogPost.Body) </div> <div class="editor-field"> @Html.EditorFor(model => model.BlogPost.Body) @Html.ValidationMessageFor(model => model.BlogPost.Body) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List","Index") </div> 解决方法
只需重命名你的动作参数:
public ActionResult Create(BlogViewModel viewModel) 有一个冲突,因为你的动作参数被称为博客,而你的视图模型(BlogViewModel)有一个名为Blog的属性.问题是默认模型绑定器不再知道在这种情况下该怎么做. 哦,如果你绝对需要你的动作参数叫做博客,那么你也可以重命名你的视图模型的博客属性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – 什么文件夹应该把我的扩展方法在ASP.Net
- ASP.NET MVC – 操纵HTTP Post
- asp.net会员提供者Guid userID
- ASP.NET ModalPopupExtender单击“事件”
- asp.net-mvc – 部分视图继承自主布局
- asp.net-mvc – 如果是流媒体,则无法发布网站
- asp.net – 我们应该使用什么序列化格式在SQL Server数据库
- IIS如何处理ASP.net MVC请求?
- asp.net – 经典Asp .asp扩展页的处理程序映射在IIS集成管道
- ASP.NET / C#:DropDownList SelectedIndexChanged事件未触
推荐文章
站长推荐
- asp.net – ‘InitializeCulture’不是其成员
- asp.net-mvc-3 – ASP.Net MVC 3剃刀:部分定义但
- asp.net – Visual Studio IDE的开发
- .NetCore技术研究-EntityFramework Core 3.0 Pre
- asp.net-mvc – 从n层ASP.Net MVC应用程序的服务
- 如何在ServiceStack服务实现中使用标准的ASP.NET
- .net – 即时生成站点地图
- asp.net-mvc – 有没有办法从控制器A调用控制器B
- asp.net三层架构增删改查
- ASP.NET Core macOS 环境配置 - ASP.NET Core 基
热点阅读