0

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>
}
2
  • Are you using IENumerable<> model? Commented Jun 13, 2013 at 5:17
  • Yes my model is - @model IEnumerable<Organization> Commented Jun 13, 2013 at 5:51

2 Answers 2

1

I have solved this by using TextBox as below

@Html.TextBox("Caption", item.GetValForProp<string>("Caption"), new { @class = "txt" })
Sign up to request clarification or add additional context in comments.

Comments

0

Wrapping in parenthesis works - notice the @( ):

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

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.