I'm really new to ASP.NET MVC and basically everything web related. Sorry if this is nooby but I'm trying to do this:
I have a ViewModel with a complex property (navigation here):
public class Request
{
public virtual BaseRequestData RequestData { get; set; }
}
BaseRequestData is abstract but has a couple classes inherited from it, which have other attributes like this one:
public class AcceleratorRequestData : BaseRequestData
{
[Display(Name="Downside amount")]
[Range(-100,0,ErrorMessage = "Downside participation must be between 0 and -100")]
[Required]
public decimal PutNotional { get; set; }
[Display(Name="Upside strike")]
[Range(1, 2, ErrorMessage = "Upside strike must be between 100% and 200%")]
public decimal CallPercentStrike { get; set; }
}
On my main 'Create' view I bind to my Request model but I want to make a partial view for my BaseRequestData depending on the type it is (for instance, AcceleratorRequestData)via the user selecting it from a dropdown. What I've tried is using some jQuery to call a controller and render a partial view depending on the dropdown. Here's one of my partial views which is a bunch of form-groups:
@model Synapse.Models.AcceleratorRequestData
@Html.ValidationSummary(true,"",new {@class = "text-danger"})
<div class="form-group">
@Html.LabelFor(m => Model.PutNotional, new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.PutNotional, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.PutNotional, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => Model.CallPercentStrike, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CallPercentStrike, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CallPercentStrike, "", new { @class = "text-danger" })
</div>
</div>
Which replaces my placeholder <div>
in my main view:
@using (Html.BeginHorizontalForm())
{
@Html.AntiForgeryToken()
<hr/>
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
<div id="accelRequestPh" style="display: none;"></div>
<input type="submit" value="Create" class="btn btn-default" />
}
But here my validation properties and bindings fail (obviously, because my BaseRequestData object isn't bound to the instance of my Request object). How can I do this? Should I use an editor template? If I do, still, how will my main view model know about them?