Beruflich Dokumente
Kultur Dokumente
NET MVC
We are going to have ClientVM that will have partial view with Address model.
Create new MVC 4 project, choose internet project and Razor.
Games Materi
Models
http://www.adambielecki.com/2014/03/submitting-form-with-partial-view-in.html 1/5
8/9/2017 Hungry developer - .NET, C#, ASP.NET MVC: Submitting form with partial view in ASP.NET MVC
taste and
public Guid clientId { get; set; } passionate game
public string firstName { get; set; } developer.
public string lastName { get; set; } View my
public Guid addressId { get; set; } complete profile
public Address addressPartial { get; set; }
2016 (8)
public ActionResult ClientInfo()
{ 2015 (23)
// Normally we would get Client from database. For test we use constructors to define new entries. 2014 (20)
2013 (39)
<li>@Html.ActionLink("Submit Partial Model", "ClientInfo", "Home")</li>
2012 (9)
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Submit Partial Model", "ClientInfo", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
Now we have to create 1 view for ClientInfo and 1 partial view for Address.
Compile your project (Shift+Ctrl+B), right click ClientInfo in Home Controller and Add View.
Check Create a strongly-type view, choose ClientVM and sca old template : Edit.
Right click on shared folder add view and call it _Address. Use the same setting as before, but this time select Address as your model class and check
box "Create as a partial view".
http://www.adambielecki.com/2014/03/submitting-form-with-partial-view-in.html 2/5
8/9/2017 Hungry developer - .NET, C#, ASP.NET MVC: Submitting form with partial view in ASP.NET MVC
@model PartialViewModelSubmission.Models.Address
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Address</h2>
<fieldset>
<legend>Address</legend>
<div class="editor-label">
@Html.LabelFor(model => model.houseNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.houseNumber)
@Html.ValidationMessageFor(model => model.houseNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.streetName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.streetName)
@Html.ValidationMessageFor(model => model.streetName)
</div>
</fieldset>
Run the application, click Submit Partial Model and submit the form.
As you can see we get error as Address have not been submitted.
We will have to create partial class for Address. In Address.cs add this partial class:
http://www.adambielecki.com/2014/03/submitting-form-with-partial-view-in.html 3/5
8/9/2017 Hungry developer - .NET, C#, ASP.NET MVC: Submitting form with partial view in ASP.NET MVC
public Guid clientId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
// public Guid addressId { get; set; }
// public Address addressPartial { get; set; }
ClientVM inherit from AddressBase partial class so we can assign address to base.
this.partialAddress = address;
This is all you have to do for classes, we have to amend views a bit as well.
@Html.Partial("_Address", Model)
In _Address view change model to AddressBase and update sca olding so it should look like that:
@model PartialViewModelSubmission.Models.AddressBase
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Address</h2>
<fieldset>
<legend>Address</legend>
<div class="editor-label">
@Html.LabelFor(model => model.partialAddress.houseNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.partialAddress.houseNumber)
@Html.ValidationMessageFor(model => model.partialAddress.houseNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.partialAddress.streetName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.partialAddress.streetName)
@Html.ValidationMessageFor(model => model.partialAddress.streetName)
</div>
</fieldset>
Now run the application again and if you followed everything correctly you should be able to see Address model when you debug.
http://www.adambielecki.com/2014/03/submitting-form-with-partial-view-in.html 4/5
8/9/2017 Hungry developer - .NET, C#, ASP.NET MVC: Submitting form with partial view in ASP.NET MVC
No comments yet
enot-poloskun Blogger
http://www.adambielecki.com/2014/03/submitting-form-with-partial-view-in.html 5/5