如何在Postback事件上读取ADO.NET实体数据模型对象中的特定字段 [英] How to read a specific field in ADO.NET Entity Data Model Object on Postback event
问题描述
我有一个从数据库中填充的下拉列表:
控制器
I have a dropdownlist that is populated from a database as:
Controller
PayrollDb pdb = new PayrollDb ();
var addBranchGui = new branch();
addBranchGui.BranchTypes = new SelectList(pdb.branchtypes,"Id", "Type");
查看
View
<p>Branch Type:@Html.DropDownListFor(x => x.BranchType, Model.BranchTypes, "Choose an option")</p>
此代码正确填充下拉列表。当用户在下拉列表中选择一个选项并提交表单并且我在[HttpPost操作方法上捕获输入数据并将其插入到数据库表中时,出现问题时,当我想要类型时它就会插入Id字段要插入的字段。
我可以理解,在Postback事件中,浏览器正在读取Id字段。有人可以指导我如何命令浏览器给我类型字段,以便我可以将其插入数据库。
This codes correctly populates the dropdownload list. Problem arises when user selects an option in the dropdownlist and submits the form and I catch the input data on [HttpPost action method and insert it into a database table, it is the "Id" field that gets inserted when I want the "Type" field to be inserted.
I can understand that on Postback event, browsers is reading "Id" field. Can someone please guide me how I can command the browser to give me the "Type" field so I can insert that into database.
推荐答案
在 SelectList中构造函数,您指定 Id 作为下拉列表的数据值字段。
In the SelectList constructor, you are specifying Id as the data value field for the dropdown.
addBranchGui.BranchTypes = new SelectList(pdb.branchtypes,"Id", "Type");
所以它会像这样渲染下拉菜单
So it will render the dropdown like this
<select name="BranchType">
<option value="2">HeadOffice
<option value="4">Branch
</option></option></select>
所以改为使用 Type 作为 datavalue 字段。
So change it to use Type as the datavalue field.
addBranchGui.BranchTypes = new SelectList(pdb.branchtypes,"Type", "Type");
这将呈现一个SELECT元素具有相同值的选项值和选项文本
This will render a SELECT element with same value for the option value and the option text
<select name="BranchType">
<option value="HeadOffice">HeadOffice
<option value="Branch">Branch
</option></option></select>
现在当用户发布表单后,您将获得BranchType字段中的文本值(类型)。
Now when user posts the form, you will get the text value(the type) in BranchType field.
这篇关于如何在Postback事件上读取ADO.NET实体数据模型对象中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!