0

How does one give a TextBoxFor a variable ID value?

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.

0

2 Answers 2

1

You can do like this

@{
    var variable = "123";
 }
@Html.TextBoxFor(item => item.OperationNo, new { id = "materialCostInput" + variable })
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.