如何在 asp .net MVC3 中为 EditorFor() html 助手设置 Id
我用它来覆盖框架生成的 id@Html.EditorFor(model => model.cost, new { id = "checkCost" })
但不幸的是 id 没有改变。即
当我使用string d = Request.Form["checkCost"];
在控制器操作中,字符串填充空值。
为什么?我还用 TextBoxFor 替换了 EditorFor 但没有任何改变,然后控制器也将 cost 作为 ID
<td >
@Html.DropDownList("check_master", "--select package--")
</td>
<td >
@Html.EditorFor(model => model.check_name)
@Html.ValidationMessageFor(model => model.check_name)
</td>
<td >
@Html.EditorFor(model => model.cost, new { id ="checkCost" })
@Html.ValidationMessageFor(model => model.cost)
</td>
更新:
- 这是我的部分视图,我的主视图(从 ajax.actionLink 渲染部分)还包含一个 EditorFor(model=>mode.cost)
- 部分视图强类型化为 Check_master 表,主视图强类型化为 package_master,其中两个实体集都拥有相同的名称属性,即其次,部分视图和主视图都保留 EditorFor“cost”
我尝试更改 check_master 属性的名称,例如 - [Column("check_cost")]
public Nullable cost { get;放; }
但不知道为什么这不起作用,即属性 naem 在引用它时没有改变
model.something
I used this to override framework generated id@Html.EditorFor(model => model.cost, new { id = "checkCost" })
But unfortunately id is not changing. i.e
When i usestring d = Request.Form["checkCost"];
at controller action then string filled with null value.
WHY?? I also replaced EditorFor with TextBoxFor but nothing changed then also controller takes cost as ID
<td >
@Html.DropDownList("check_master", "--select package--")
</td>
<td >
@Html.EditorFor(model => model.check_name)
@Html.ValidationMessageFor(model => model.check_name)
</td>
<td >
@Html.EditorFor(model => model.cost, new { id ="checkCost" })
@Html.ValidationMessageFor(model => model.cost)
</td>
Updated:
- This is my partial view and my main view(rendering partial from ajax.actionLink) also hold a EditorFor(model=>mode.cost)
- Partial view is strongly typed to Check_master table and main view is strongly typed to package_master where both entitySet hold same name property i.e "COST" Secondly both partial and main view hold EditorFor "cost"
I tried to change the name of check_master property like by
- [Column("check_cost")]
public Nullable cost { get; set; }
But dont know why this is not working i.e property naem is not changing while referring it like
model.something
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建 2 个具有 CheckCost 和 PackageCost 属性的视图模型,这样就可以进行分离:
这样可以避免冲突。
Create 2 view models with properties CheckCost and PackageCost that way there's a seperation:
This way avoid conflicts.
要具体回答所提出的问题,请尝试以下操作:
注意细微(但重要)的差异:... new { @id = "checkCost" })
To specifically answer the question asked, try this:
Note the slight (but important) difference: ... new { @id = "checkCost" })