mmssaann
mmssaann

Reputation: 1507

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>
}

Upvotes: 0

Views: 1093

Answers (2)

combatc2
combatc2

Reputation: 1261

Wrapping in parenthesis works - notice the @( ):

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

Upvotes: 0

mmssaann
mmssaann

Reputation: 1507

I have solved this by using TextBox as below

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

Upvotes: 1

Related Questions