UpdateModel 未通过 ViewModel 和 DropDownListFor 的属性更新模型
我正在尝试设置一个编辑视图,其中有一个文本框和 DropDownListFor。我已经找到了一种填充 DDLF 的方法,并且渲染和发布的值是正确的,但我似乎无法让模型正确更新。
我尝试更新的对象是从 LINQtoSQL 生成的,并且在数据库中它具有外键列。在导致“包含”关系的 LINQtoSQL 类中。我可以获得代表数据库中列的 ID 属性,以及它代表的对象。
zupanija = new Zupanija(); //object that needs to be updated
zupanija.Drzava; //object that i want to change to make the update
zupanija.DrzavaID; //Property linked to object that should change
我想出进行更新的唯一方法是从 DDLF 获取值并使用它来获取我想要更改的对象,如下所示:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var zupanija = repo.ZupanijaById(id);
var drzava = new repoDrzava().DrzavaById(Convert.ToInt32(collection["Zupanija.DrzavaID"]));
zupanija.Drzava = drzava;
}
另外,当我尝试像这样更新 ID 字段时,我收到以下错误:
zupanija.DrzavaID = Convert.ToInt32(collection["Zupanija.DrzavaID"]);
错误:抛出新的System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
在我看来,这是非常糟糕的方法,我正在尝试让UpdateModel 工作。
I am trying to set up an Edit view on which I have a text box and DropDownListFor. I have figured out a way to populate the DDLF, and the rendered and posted values are correct, but i cant seem to get the model to update properly.
The object i am trying to update is generated from LINQtoSQL, and in database it has foreign key column. In LINQtoSQL class that resulted in "Contains" relationship. I can get to ID property that represents the column in DB, and also the object that it represents.
zupanija = new Zupanija(); //object that needs to be updated
zupanija.Drzava; //object that i want to change to make the update
zupanija.DrzavaID; //Property linked to object that should change
Only way i have figured out to do the update is to get the value from DDLF and use it to get the object that i want to change like this:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var zupanija = repo.ZupanijaById(id);
var drzava = new repoDrzava().DrzavaById(Convert.ToInt32(collection["Zupanija.DrzavaID"]));
zupanija.Drzava = drzava;
}
Also when i try to update the ID field like this, then i get the folowing error:
zupanija.DrzavaID = Convert.ToInt32(collection["Zupanija.DrzavaID"]);
Error: throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
This seems to me that it is very lousy way to do this, and i am trying to get UpdateModel to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在寻找其他东西时找到了解决方案,在 Joe Stevens 的博客中:
使用 ViewModel 时使用控制器 UpdateModel
问题如下:当使用视图模型时,要正确绑定属性,有必要“指示“ UpdateModel 帮助程序如何找到我们希望更新的实际类。
我的解决方案需要修改,
因为我使用的 ViewModel 类包含几个属性以及我想要更新的主要数据类。
这是代码,希望有助于理解:
I have found the solution while looking for something else, in blog by Joe Stevens:
Using Controller UpdateModel when using ViewModel
The catch is in following: When view model is used then to correctly bind the properties it is necessary to "instruct" the UpdateModel helper how to find the actual class we wish to update.
My solution required to modify
Because i was using a ViewModel class that contained couple properties along with the main data class i wanted to update.
Here is the code, i hope it helps to understand:
下拉列表由 HTML 表单中的
包含
标签列表,每个标签包含一个 ID 和一个文本。当用户选择一个选项并提交表单时,该选项的相应 ID 会被 POST 到服务器。而且只有身份证。因此,您可以在
Edit
POST 操作中获得的只是所选选项的 ID。UpdateModel
所做的就是使用发送的请求参数并将它们转换为强类型对象。但因为 POST 的只是一个简单的 ID,所以您只能得到它。从那时起,如果您想获取相应的模型,则必须使用此 ID 查询数据存储。所以你无法得到不存在的东西。A dropdown list is represented by a
<select>
tag in an HTML form. A<select>
contains a list of<option>
tags each containing an ID and a text. When the user selects an option and submits the form the corresponding ID of this options is POSTed to the server. And only the ID. So all you can expect to get in yourEdit
POST action is the ID of the selected option. And all thatUpdateModel
does is use the request parameters that are sent and convert them to a strongly typed object. But because all that is a POSTed is a simple ID that's all you can get. From there on you have to query the datastore using this ID if you want to obtain the corresponding model. So you cannot get something that is not existing.