【问题标题】:Call method in html input text with parameters + razor MVC4在带有参数的html输入文本中调用方法+ razor MVC4
【发布时间】:2013-06-09 09:32:17
【问题描述】:

我在我的剃刀视图 (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>
}

【问题讨论】:

  • 你使用的是IENumerable&lt;&gt;模型吗?
  • 是的,我的模型是 - @model IEnumerable

标签: asp.net-mvc-4 razor-2


【解决方案1】:

我已经通过使用下面的 TextBox 解决了这个问题

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

【讨论】:

    【解决方案2】:

    用括号括起来有效 - 注意@( ):

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

    【讨论】:

      猜你喜欢
      • 2011-11-25
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      相关资源
      最近更新 更多