如何在 asp .net MVC3 中为 EditorFor() html 助手设置 Id

发布于 2024-12-22 12:58:12 字数 1158 浏览 1 评论 0原文

我用它来覆盖框架生成的 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 use
string 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-12-29 12:58:12

创建 2 个具有 CheckCost 和 PackageCost 属性的视图模型,这样就可以进行分离:

public class CheckViewModel{
   public string CheckCost{get;set;}
}

public class PackageViewModel{
   public string PackageCost{get;set;}
}

这样可以避免冲突。

Create 2 view models with properties CheckCost and PackageCost that way there's a seperation:

public class CheckViewModel{
   public string CheckCost{get;set;}
}

public class PackageViewModel{
   public string PackageCost{get;set;}
}

This way avoid conflicts.

泪眸﹌ 2024-12-29 12:58:12

要具体回答所提出的问题,请尝试以下操作:

@Html.EditorFor(model => model.cost, new { @id = "checkCost" })

注意细微(但重要)的差异:... new { @id = "checkCost" })

To specifically answer the question asked, try this:

@Html.EditorFor(model => model.cost, new { @id = "checkCost" })

Note the slight (but important) difference: ... new { @id = "checkCost" })

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文