MVC4中的剃刀引擎问题

时间:2015-01-27 07:36:08

标签: asp.net-mvc-4 razor

当我在视图页面中声明文本框时,将出现以下错误

 The type arguments for method 'System.Web.Mvc.Html.InputExtensions.TextBoxFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>, 
System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>)'
 cannot be inferred from the usage. Try specifying the type arguments
 explicitly

即使我包括

<compilation debug="true" targetFramework="4.0">
  <!-- ... -->
</compilation>

这在webconfig文件中 但同样的错误表明。 ............ 我的代码

 @Html.TextBoxFor(x => x.Entity, new { @id = "Entityname" })

// .......... 模型

public string Entity { set; get; }

// ......... // .............. .cshtml页面

  @model BOSSNew.Models.NewQuantifierM
    @{Layout = "../Shared/_Layout.cshtml";}
    <div class="breadCrumbHolder">
        @{Html.RenderAction("BreadCrumb", "Base", new { menulist = new string[] { "Quantifier", "New Quantifier" }, CurrentURL = new string[] { "#", "#" } });}
    </div>
    <div class="divContentPane">
        <div class="contentPaneHead">
            <span class="contentPaneTitle">Users Details </span>
        </div>
        <table class="ClsTable ClsPad0">
            <tr class="even">
                <th>@LabelHelper.GetLabel("THLentity", 3)
                </th>
                <td>
                    @Html.TextBoxFor(x => x.Entity, new { @id = "Entityname" })
                    <img title="" id="selectentit" style="margin: 5px" onclick="getentity('txtentity','optentity')"
                        alt="" src="../../../Name.Controls/Themes/Name-Theme/images/entity.png">
                </td>
            </tr>
     </table>
    </div>

// .............

有什么想法吗?

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您尚未为视图定义模型,因此无法使用

  

x =&gt; x.Field

表达。

看起来应该或多或少:

<强> SomeView.cshtml

@model SomeModel
@{Layout = "../Shared/_Layout.cshtml";}
    <div class="breadCrumbHolder">
        @{Html.RenderAction("BreadCrumb", "Base", new { menulist = new string[] { "Quantifier", "New Quantifier" }, CurrentURL = new string[] { "#", "#" } });}
    </div>
    <div class="divContentPane">
        <div class="contentPaneHead">
            <span class="contentPaneTitle">Users Details </span>
        </div>
        <table class="ClsTable ClsPad0">
            <tr class="even">
                <th>@LabelHelper.GetLabel("THLentity", 3)
                </th>
                <td>
                    @Html.TextBoxFor(x => x.Entity, new { @id = "Entityname" })
                    <img title="" id="selectentit" style="margin: 5px" onclick="getentity('txtentity','optentity')"
                        alt="" src="../../../Name.Controls/Themes/Name-Theme/images/entity.png">
                </td>
            </tr>
     </table>
    </div>

<强> SomeModel.cs

public class SomeModel
{
   public string Entity { set; get; }
}

最后在你的行动方法......

public ActionResult SomeMethod()
{
   var model = new SomeModel();
   //here fill the entity field
   return View(model);
}