ASP.NET MVC中添加记录,从单一的视图相关的多个表 [英] ASP.NET MVC Adding records to related multiple tables from single view

查看:168
本文介绍了ASP.NET MVC中添加记录,从单一的视图相关的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的MVC所以我道歉,如果我没有正确/解释我的要求还没有使用正确的术语。

I'm very new to MVC so i apologize if i haven't explained my requirement properly/haven't used correct terminology.

我有 项目 详细信息实体一个简单的数据库。他们有一个一对多的关系,使项目有很多细节。

I have a simple database with item and detail entities. They have a one-to-many relationship so Item has many Details.

我想记录:从一个单一的视图添加到这两个表。

I'd like to add records to both these tables from a single view.

到目前为止,我的理解是,如果我添加了一个创建行动'项目',它会得到在HTTPPost请求中的'项目'的对象。这是否意味着我必须创造一个细节单独行动/看法?

So far my understanding is that if i add a create action for 'item' it will get an 'item' object in the HTTPPost request. So does that mean i have to create a separate action/view for details?

或者是局部视图'我应该在这里使用?是否有人可以给我一个例子。真的AP preciate它。谢谢你。

Or is 'partial-view' what i should use here? Can someone please give me an example. Really appreciate it. Thanks.

推荐答案

这听起来像你使用强类型的意见,并采取了一些MVC魔力,自动填充在行动中这种类型的优势。我没有用MVC3还,但在MVC2,你可能会使用的UpdateModel自动填充从你的FormCollection类型。无论是MVC是做一些魔术为您或没有,你总是会从您的观点将信息发送到您的控制器(无论是发布或URL字符串入门) - 你永远可以访问的URL字符串,或形成集合原始值。

It sounds like you're using strongly typed views, and taking advantage of some MVC magic that auto-populates that type in action. I haven't used MVC3 yet, but in MVC2, you'd probably use UpdateModel to auto-populate your type from the formcollection. Whether MVC is doing some magic for your or not, you will always be sending information from your view to your controller (either POSTing or GETting with a url string) - you'll always be able to access the url string, or forms collection for the raw values.

话虽这么说,你可能想创建一个视图模型 - 这是适合您的视图需求的典范。在你的情况下,它仅仅是一个复合型。这个视图模型,仅仅是一个普通类。它的的作为每个类型的暴露为这个新类的属性一样简单。如果你这样做,你不要忘了preFIX类/右的属性,如:
Model.Item.SomeProperty,Model.Detail.SomeOtherProperty
如果你不这样做,MVC可能将无法映射回来了正确的值。

That being said, you probably want to create a ViewModel - that is a model that fits the needs of your view. In your case it is just a compound model. This ViewModel, is just a plain class. It should be as easy as exposing each of your types as a property of this new class. If you do that, you'll don't forget to prefix your class/properties right, like: Model.Item.SomeProperty, Model.Detail.SomeOtherProperty If you don't, MVC probably won't be able to map values coming back correctly.

这应该让你在你的控制器需要的数据 - 那么你只需要插入到数据库中。

That should get you the data you need in your controller - then you just have to insert into the database.

这篇关于ASP.NET MVC中添加记录,从单一的视图相关的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆