asp.net-mvc


posting data from partial view on main view then submitting to controller


I have a MVC model as follows
public class ListSampleModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int SampleId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<PointOfContact> lstPoc { get; set; }
}
public class PointOfContact
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PocID { get; set; }
public string EmailAddress { get; set; }
public string PhoneNumber { get; set; }
}
What I have done is, create "PointOfContact" as a partial view on a jquery dialog and on "save" button click it shows the data on the main view in labels (I will have multiple point of contacts), now on submit I want this data along with the property values of ListSampleData to be posted back to the controller.
The issue is, the data related to simple properties are returned back but the list is always null.
below is my View
#model MVCDataFlowSample.Models.ListSampleModel
#using (Html.BeginForm("Create", "ListSample", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>ListSampleModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div id="dialog1" class="ui-dialog" style="background-color:gray;"></div>
<div id="data">
</div>
<p>
<input type="button" value="Add More..." id="btnAdd" />
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Javascript on Main View
<script type="text/javascript">
$(document).ready(function () {
$('#btnAdd').on('click', function () {
$('#dialog1').dialog({
autoOpen: true,
position: { my: "center", at: "center", of: window },
width: 1000,
resizable: false,
title: 'Add User Form',
modal: true,
open: function () {
$(this).load('#Url.Action("PocPartial", "ListSample")');
},
buttons: {
"Save User": function () {
addUserInfo();
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
return false;
});
function addUserInfo(email, phone) {
var text = "<div id='EmailAddress'>Email Address:" + $("#EAddress").val() + "</div><div id='PhoneNumber'>Phone Number:" + $("#PhNo").val() + "</div>";
$("#data").append(text);
}
});
</script>
Partial View
#model MVCDataFlowSample.Models.PointOfContact
<div>
#Html.Label("EmailAddress:")
<div>
#Html.TextBoxFor(x => x.EmailAddress, new { id = "EAddress" })
</div>
#Html.Label("PhoneNumber:")
<div>
#Html.TextBoxFor(x => x.PhoneNumber, new { id = "PhNo" })
</div>
</div>
any help will be appreciated.
The contents of DIV elements are not submitted as form data. If you'd like that data to be submitted, add it to the DOM as hidden INPUT elements in addition to your DIVs. You'll also need to format their names correctly so that MVC knows how to bind them. See this article for how complex objects are bound in MVC.
I posted partial view data to the main page's post action You can modify the idea to any of your suits
Partial View
<select name="Country">
<option>Indian</option>
<option>Africa</option>
</select>
<select name="State">
<option>Kerala</option>
<option>TamilNadu</option>
</select>
<select name="City">
<option>Thrissur</option>
<option>Palakkad</option>
</select>
Index Page
#{
ViewBag.Title = "IndexTestPost";
}
<h2>IndexTestPost</h2>
#using(Html.BeginForm()){
#Html.Partial("~/Views/_PartialPagePostCountry.cshtml");
<input type="submit" />
}
Class To Catch Post Data
public class CountryCityState
{
public string Country { get; set; }
public string State { get; set; }
public string City { get; set; }
}
Controller
public class TestPostPartialController : Controller
{
// GET: TestPostPartial
public ActionResult IndexTestPost()
{
return View();
}
[HttpPost]
public ActionResult IndexTestPost(CountryCityState CtnCtySta)
{
return View();
}
}

Related Links

Why is MVC page compilation forcing constant app restarts on my Azure web app?
Bootstrap Modal in separate view [duplicate]
asp.net core authentication with IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
Access Linq Output in MVC Controller itself
Call a controller method with parameters from partial view mvc 5
How do I publish my MVC site on IIS 7+ with proper URLs?
.ASPNET.Cookies cookie is not creating during Azure AD authentication [duplicate]
Save List of object in MVC 5
Using Automapper to update an existing Entity with less Properties
Entity Framework 6 DropCreateDatabaseIfModelChanges
Optgroup in DropDownlistFor MVC 4 Not Support Mvc validation
MS Edge Removes Space From String Return From MVC
MVC Entity Framework Error on Mapping Entity with [NotMapped] properties
ASP.NET MVC 5 - Databind with bootstrap form helpers
Entity Framework code first - foreign key relationship
How do I get rid of these remapping warnings in a brand new MVC project after updating all nuget packages?

Categories

HOME
dynamic-programming
copy
pyqt5
appium-ios
android-wear
jsonschema
android-sms
wso2is
httpbuilder-ng
payment-processing
telnet
zebble
frequency
scope
command-line-arguments
lighttpd
axios
collision
bitcode
background-subtraction
memorystream
operator-keyword
gecko
breakpoint-sass
scom
lync
azure-api-apps
lifecycle
chamilo-lms
caldroid
bem
jsonstore
symfony-process
backwards-compatibility
ganglia
soql
python-module
manual
getelementbyid
asp.net-mvc-custom-filter
ifc
vs-2015-preview
file-management
app-engine-ndb
target-platform
uploadify
radix-sort
uat
hjson
chunk-templates
emberfire
mkpolyline
getuikit
epplus-4
centering
typesafe-activator
symfony-2.5
eventmachine
short
asp.net-apicontroller
sonata-media-bundle
mod-python
libnids
guice-servlet
arbtt
memento
timestamp-with-timezone
calloc
ria
httpconnection
livevalidation
payment-services
boost-gil
sdl.net
facebook-authentication
sharepoint-timer-job
regexbuddy
keylogger
high-traffic
clipboard-pictures

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App