使用参数+ razor MVC4在html输入文本中调用方法

问题描述 投票:0回答:1

我在我的剃刀视图(list.cshtml)中将属性值分配给html输入测试框,如下所示

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

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

这很好用。

但是,如果我想写下面的内容:

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

它给出了编译错误,无法识别“Caption”参数。如果我给出单引号,则不会将其视为参数,从而给出无效参数的异常。

我怎么能纠正这个?

代码块:

@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>
}
asp.net-mvc-4 razor-2
1个回答
1
投票

我已经通过使用TextBox解决了这个问题,如下所示

@Html.TextBox("Caption", item.GetValForProp<string>("Caption"), new { @class = "txt" })
© www.soinside.com 2019 - 2024. All rights reserved.