简体   繁体   English

如何将变量/自定义id值设置为Html.TextBoxFor-MVC,C#,Razor

[英]how to set variable/custom id value to Html.TextBoxFor - MVC, C#, Razor

How does one give a TextBoxFor a variable ID value? 如何给TextBoxFor变量ID值?

Example Attempt: 尝试示例:

 @var variable = model.codeOne;

<div>
@Html.TextBoxFor(item => item.OperationNo, new { id = "@("materialCostInput"+ 
variable)" })
</div>

I have looked around the forum for an answer to this question, but have yet to find a similar question. 我在论坛上四处寻找有关此问题的答案,但尚未找到类似的问题。

You can do like this 你可以这样

@{
    var variable = "123";
 }
@Html.TextBoxFor(item => item.OperationNo, new { id = "materialCostInput" + variable })

This is a more complete answer: 这是一个更完整的答案:

public class SuperModel
{
    public string OperationNo { get; set; }

}

//Create an edmx to your table
public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Index2003(SuperModel sm, FormCollection formCollection)
    {
        var theId = formCollection.Keys[0];
        return View();
    }

    public ActionResult Index2003()
    {
        SuperModel sm = new SuperModel { OperationNo = "op1" };
        return View(sm);
    }

Controller: 控制器:

@model Testy20161006.Controllers.SuperModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index2003</title>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            var variable = "456";
            @Html.TextBoxFor(item => item.OperationNo, new { Name = "materialCostInput" + variable })
            <input type="submit" value="submit" />
        }
    </div>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM