1

I'm quite new about MVC. I have the following Model classes:

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; }
}

And i want use this model in my view:

@model Store

@Html.TextBoxFor(model => ((Silo2Store)Model.storeData).S3) 

The corresponding Controller method is:

public ActionResult Customer()
{
    using (Store t = (Store)Session["Store"])
    {
        if (t.PriceListInfo == null)
        {
            t.PriceListInfo = new PriceList();
        }
        t.PriceListInfo.codice = "XXX";
        return View(t);
    }
}

And I'd like to retrieve the model in my Controller:

[HttpPost]
public ActionResult Customer(Store modelStore)
{
    var test = ((Silo2Store)Model.storeData).S3;
}

but Model.storeData attribute isn't initialized in my view, it's null. Then, I can't retrieve the value in my controller.

Should I change my model in anyway?

6
  • 2
    Can you show us the code of your GET controller method? Commented Mar 7, 2013 at 9:04
  • ' public ActionResult Customer() { using (Store t = (Store)Session["Store"]) { if (t.PriceListInfo == null) { t.PriceListInfo = new PriceList(); } t.PriceListInfo.codice = "XXX"; return View(t); } }' Commented Mar 7, 2013 at 9:25
  • ..When I check my Model in the view (in debug mode), the object "storeData" isn't null. It's NULL in controller side. Commented Mar 7, 2013 at 9:30
  • I think you'll have to define your own model binder for IStore. This article may be helpful as a starting point... Commented Mar 7, 2013 at 9:37
  • YES!....I've created my model binder. It seems to work now. Tnx. Commented Mar 7, 2013 at 11:07

1 Answer 1

1

You have to define your own model binder for IStore.

Taken from this article on MSDN Magazine about MVC Model Binding:

For example, even though the Microsoft .NET Framework provides excellent support for object-oriented principles, the DefaultModelBinder offers no support for binding to abstract base classes and interfaces.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.