MVC EF邮政储蓄类型和客户类型在一个 [英] MVC EF saving post type and customer type in one
问题描述
我有一个名为博文
与属性的虚拟参考输入客户类型
。
public虚拟客户客户{搞定;组; }
在我的表单我发布博文
和客户
数据传输到控制器。所有值都被接收。然而,当我使用的SaveChanges
的客户
数据不会被保存。
db.BlogPosts.Add(博客);
db.SaveChanges();
所以我想在博文
数据保存到表博文
(工作),但客户表遗体空的。
公众的ActionResult WriteBlog(Models.BlogPost博客,HttpPostedFileBase blogImage,HttpPostedFileBase blogLogo)
{db.BlogPosts.Add(博客);
db.SaveChanges();返回查看();
}
只要你设置客户ID
/ 客户
属性值的博文
实体对象,它应该为客户节省没有任何问题。所以我假设你的问题出在你的Razor视图,它不发送正确的数据回HttpPost操作方法。
看起来您使用的是实体框架直接在视图生成的实体类。这通常是还不如现在你的观点是紧耦合的这个实体一个很好的做法。理想情况下,你应该使用视图特定视图模型类将数据从视图转移到你的操作方法,并在那里你会读出贴视图模型对象(通过MVC Modelbinding从表格数据创建的)的属性值,创建一个实体对象,设置的属性值并保存它。
要启动,创建一个这样的视图模型
公共类CreatePostViewModel
{
公众诠释标识{设置;得到; }
[需要]
公共字符串名称{设置;得到; }
[需要]
公共字符串客户名称{设置;得到; }
公共HttpPostedFileBase BlogImage {设置;得到; }
公共HttpPostedFileBase BlogLogo {设置;得到; }
}
请您razorview强类型这个视图模型
@model CreatePostViewModel
@using(Html.BeginForm(创建,邮报,FormMethod.Post,
新{ENCTYPE =的multipart / form-data的})){
<标签>职衔和LT; /标签> @ Html.TextBoxFor(S = GT; s.Title)
<标签>客户与LT; /标签> @ Html.TextBoxFor(S = GT; s.CustomerName)
<标签>博客图片< /标签> <输入类型=文件名称=BlogImage/>
<标签>博客logo及LT; /标签> <输入类型=文件名称=BlogLogo/> <输入类型=提交/>
}
而在你的HttpPost操作方法
[HttpPost]
公众的ActionResult创建(CreatePostViewModel模型)
{
如果(ModelState.IsValid)
{
//要获得文件,访问model.BlogImage&安培; model.BlogLogo性质
使用(VAR DB =新YourDbContext())
{
VAR的客户=新客户();
customer.Name = model.CustomerName;
//设置其他属性值,以及 VAR博客=新的博文();
blog.Title = model.Title;
blog.Customer =客户;
//设置其他属性值,以及 db.Blogs.Add(博客);
db.SaveChanges();
}
}
返回查看(模型);
}
I have a type called blogpost
with a property virtual reference to type customer
.
public virtual Customer Customer { get; set; }
In my form I post the blogpost
and customer
data to a controller. All values are received. However when I use SaveChanges
the customer
data is not saved.
db.BlogPosts.Add(blog);
db.SaveChanges();
So I want the blogpost
data to be saved to table blogpost
(which works) but the customer table remains empty.
public ActionResult WriteBlog(Models.BlogPost blog, HttpPostedFileBase blogImage, HttpPostedFileBase blogLogo)
{
db.BlogPosts.Add(blog);
db.SaveChanges();
return View();
}
As long as you are setting the CustomerId
/Customer
property value to the BlogPost
entity object, It should save the customer without any problem. So i am assuming that your problem lies in your razor view and it is not sending the correct data back to the HttpPost action method.
It looks like you are using the entity class generated by entity framework directly in the view. This is generally not a good practice as now your view is tightly coupled to this entity. Ideally you should be using a view specific viewmodel class to transfer data from your view to your action method and there you will read the posted viewmodel object(created by MVC Modelbinding from form data) property values, create an entity object, set the property values and save it.
To start, create a viewmodel like this
public class CreatePostViewModel
{
public int Id { set; get; }
[Required]
public string Title { set; get; }
[Required]
public string CustomerName { set; get; }
public HttpPostedFileBase BlogImage { set; get; }
public HttpPostedFileBase BlogLogo { set; get; }
}
Make your razorview strongly typed to this viewmodel
@model CreatePostViewModel
@using (Html.BeginForm("Create", "Post", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label>Post title</label> @Html.TextBoxFor(s=>s.Title)
<label>Customer </label> @Html.TextBoxFor(s => s.CustomerName)
<label>Blog Image</label> <input type="file" name="BlogImage" />
<label>Blog Logo </label> <input type="file" name="BlogLogo" />
<input type="submit"/>
}
And in your HttpPost action method
[HttpPost]
public ActionResult Create(CreatePostViewModel model)
{
if (ModelState.IsValid)
{
// To get the files, access model.BlogImage & model.BlogLogo properties
using(var db=new YourDbContext())
{
var customer = new Customer();
customer.Name = model.CustomerName;
//Set other property values as well
var blog = new BlogPost();
blog.Title = model.Title;
blog.Customer = customer;
//Set other property values as well
db.Blogs.Add(blog);
db.SaveChanges();
}
}
return View(model);
}
这篇关于MVC EF邮政储蓄类型和客户类型在一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!