serlingpa
serlingpa

Reputation: 12690

Properties in a base class not appearing in view model

I have a base class which looks a little like this:

public class EformBase
{
    public decimal Latitude { get; set; }
    public decimal Longitude {get;set;}
    // lots of other properties here
}

and a derived class which looks like this:

public class GraffitiViewModel : EformBase
{
    public RadioButtonList<YesNoType> GraffitiOffensive { get; set; }
    public RadioButtonList<YesNoType> GraffitiTag { get; set; }
    // other properties here
}

I am using this class as a view model in MVC. Unfortunately, when the view model is posted back, the properties in the base class are not appearing in the view model. Like this:

[HttpPost]
public ActionResult Index(GraffitiViewModel vm)
{
   // add to database code
}

The vm, when posted back to this action, does not include the longitude and latitude as specified in the base class. What am I missing? Do I have to do something special for the base class to be included?

Upvotes: 3

Views: 1636

Answers (1)

Vlince
Vlince

Reputation: 6011

Unfortunately, your post does not mention if you are (or not) using the two properties (Latitude and Longitude) inside your View.

In addition, when you mention: Unfortunately, when the view model is posted back, the properties in the base class are not appearing in the view model.

Do you mean that you do NOT see the two properties?

OR

Do you mean that you see the two properties but the values are null?

Consider the following code example:

ViewModel and Base class:

public class MyViewModel : MyBaseViewModel
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
}

public class MyBaseViewModel
{
    public string Z { get; set; }
}

The Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel myViewModel)
    {
        var z = myViewModel.Z;
        return null;
    }
}

The View:

@model MvcApplication1.Models.MyViewModel

@using (Html.BeginForm()) {
    <fieldset>
        <legend>MyViewModel</legend>
        <div class="editor-label">@Html.LabelFor(model => model.A)</div>
        <div class="editor-field">@Html.EditorFor(model => model.A)</div>

        <div class="editor-label">@Html.LabelFor(model => model.B)</div>
        <div class="editor-field">@Html.EditorFor(model => model.B)</div>

        <p><input type="submit" value="Create" /></p>
    </fieldset>
}

Notice how the view only is only using property A and property B.

When I submit the <form> and place a breakpoint on my [HttpPost] method inside my Controller, I can view the values I entered for the textbox A and textbox B.

But since I do not have a textbox C and a textbox D. These properties will be set to null.

Likewise for property Z which happens to be defined in my Base class.

Because property Z is not used in my <form>, when I submit it, I’ll have null as a value.

In order to view property Z when you submit the <form>, you’d have to add it inside your <form>...</form> like so:

<div class="editor-label">@Html.LabelFor(model => model.Z)</div>
<div class="editor-field">@Html.EditorFor(model => model.Z)</div>

Upvotes: 5

Related Questions