Razor MVC4:模型中的接口对象
我对MVC很陌生。我有以下模型类:Razor MVC4:模型中的接口对象,razor,asp.net-mvc-4,Razor,Asp.net Mvc 4,我对MVC很陌生。我有以下模型类: public class Store { public PriceList PriceListInfo { get; set; } public IStore storeData; } public class PriceList { public int id { get; set; } public string codice { get; set; } } public interface IStore { [
public class Store
{
public PriceList PriceListInfo { get; set; }
public IStore storeData;
}
public class PriceList
{
public int id { get; set; }
public string codice { get; set; }
}
public interface IStore
{
[...]
}
public class Silo2Store : IStore
{
public int S2 { get; set; }
public int S3 { get; set; }
}
我想在我看来使用这个模型:
@model Store
@Html.TextBoxFor(model => ((Silo2Store)Model.storeData).S3)
相应的控制器方法为:
public ActionResult Customer()
{
using (Store t = (Store)Session["Store"])
{
if (t.PriceListInfo == null)
{
t.PriceListInfo = new PriceList();
}
t.PriceListInfo.codice = "XXX";
return View(t);
}
}
我想在我的控制器中检索模型:
[HttpPost]
public ActionResult Customer(Store modelStore)
{
var test = ((Silo2Store)Model.storeData).S3;
}
但是Model.storeData
属性在我的视图中没有初始化,它是null
。然后,我无法检索控制器中的值
我是否应该更改我的模型?您必须为
IStore
定义自己的模型绑定器
摘自:
例如,尽管Microsoft.NET Framework为面向对象原则提供了极好的支持,但DefaultModelBinder不支持绑定到抽象基类和接口
您必须为
IStore
定义自己的模型绑定器
摘自:
例如,尽管Microsoft.NET Framework为面向对象原则提供了极好的支持,但DefaultModelBinder不支持绑定到抽象基类和接口
你能给我们看一下你的GET controller方法的代码吗?'public ActionResult Customer(){使用(Store t=(Store)Session[“Store”]{如果(t.PriceListInfo==null){t.PriceListInfo=new PriceList();}t.PriceListInfo.codice=“XXX”;return View(t);}}}’…当我在视图中检查模型时(在调试模式下),对象“storeData”不是空的。控制器端为空。我认为您必须为
IStore
定义自己的模型绑定器。作为一个起点可能会有所帮助…是的!。。。。我已经创建了我的模型活页夹。它现在似乎起作用了。你能给我们看一下你的GET controller方法的代码吗?'public ActionResult Customer(){using(Store t=(Store)Session[“Store”]{if(t.PriceListInfo==null){t.PriceListInfo=new PriceList();}t.PriceListInfo.codice=“XXX”;return View(t);}}}’…当我在视图中检查模型时(在调试模式下),对象“storeData”不是空的。控制器端为空。我认为您必须为IStore
定义自己的模型绑定器。作为一个起点可能会有所帮助…是的!。。。。我已经创建了我的模型活页夹。它现在似乎起作用了。Tnx。