7

我在不同项目(类库)中的视图模型。我添加了参考。但是当我从我的 mvc4 视图中调用它时,比如@model Fancy.Management.Model.Home.IndexModel。查看不认识它。我不知道是什么问题。我的视图如下所示:

@model Fancy.Management.Model.Home.IndexModel
<html>
<head>
    <title>Giriş</title>
</head>
<body>
   @Html.BeginForm(){
     <table>
        <tr>
            <td>Kullanıcı Adı:</td>
            <td>@Html.TextBoxFor(m=>m.UserName)</td>
        </tr>
        <tr>
            <td>Şifre:</td>
            <td>@Html.PasswordFor(m=>m.Password)</td>
        </tr>
         <tr>
             <td></td>
             <td><input type="submit" value="Giriş" /></td>
         </tr>
    </table>
    }
</body>
</html>

我的控制器如下图所示:

namespace Fancy.Web.Management.Controllers
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
#endregion

public class HomeController : Controller
{
    #region Get
    public ActionResult Index()
    {
        return View();
    } 
    #endregion

}
}

我的模型如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fancy.Management.Model.Home
{
public class IndexModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
}
}
4

2 回答 2

13

您向模型添加了参考?好的,这还不足以让您的视图看到它。

首先,打开文件Views夹中的 web.config 文件。然后,为您的模型类添加一个命名空间标签,如下所示:

<system.web.webPages.razor>
// ...
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    // ...
    <add namespace="Fancy.Management.Model" />
    // ...
  </namespaces>
</pages>

最后改变你@model的看法,如下所示:

@model Home.IndexModel

问题应该消失了...

于 2013-07-21T10:09:41.857 回答
2

像这样使用

 public class HomeController : Controller
 {
   #region Get
   public ActionResult Index()
   {
        IndexModel inModel=new IndexModel();
        return View(inModel);
   } 
   #endregion

 }
}
于 2013-07-21T10:15:25.550 回答