asp.net-mvc-4razor-2

Call method in html input text with parameters + razor MVC4


I am assigning value of property to html input test box like below in my razor view (list.cshtml)

string  cap = item.GetValForProp<string>("Caption");

input type="text" name="Caption" class="txt" value="@cap"

This works fine.

However if I want to write it something like below:

input type="text" name="Caption" class="txt" value="@item.GetValForProp<string>("Caption")"

It is giving compilation error not recognizing "Caption" parameter. If I give single quotes, it is not considering that as parameter, giving exception that invalid arguments.

How can I correct this?

Block of code:

@foreach (var item in Model) {

     cap = item.GetValForProp<string>("Caption");
     nameinuse = item.GetValForProp<string>("NameInUse");
     desc = item.GetValForProp<string>("Description");

    <tr>
        <td class="txt">
            <input type="text" name="Caption" class="txt" value="@cap"/>
            <input type="text" name="Caption" class="txt" value="@nameinuse"/>
            <input type="text" name="Caption" class="txt" value="@desc"/>
        </td>

    </tr>
}

Solution

  • I have solved this by using TextBox as below

    @Html.TextBox("Caption", item.GetValForProp<string>("Caption"), new { @class = "txt" })