This is my controller code. I have one view and inside that i have one partial view. At the end in partial view i have one submit button. When i click this button i want to save data of main view and partial view to thier respective tables.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
using System.Data.Entity.Validation;
namespace MvcApplication2.Controllers
{
public class HomePageController : Controller
{
//
// GET: /HomePage/
MVCDemoEntities db = new MVCDemoEntities();
public ActionResult Submit()
{
List<DocTypeMaster> alldoclist = new List<DocTypeMaster>();
using (MVCDemoEntities db = new MVCDemoEntities())
{
alldoclist = db.DocTypeMasters.OrderBy(a => a.DocTypeName).ToList();
}
ViewBag.docid = new SelectList(alldoclist, "Id", "DocTypeName");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit(DetailsEntry de)
{
List<DocTypeMaster> alldoclist = new List<DocTypeMaster>();
using (MVCDemoEntities db = new MVCDemoEntities())
{
alldoclist = db.DocTypeMasters.OrderBy(a => a.DocTypeName).ToList();
}
ViewBag.docid = new SelectList(alldoclist, "Id", "DocTypeName", de.DocumentId);
return View(de);
}
[HttpGet]
public PartialViewResult RacersByCountryPartial(string id)
{
Passport ps = new Passport();
Pan pn = new Pan();
string id1 = id.Trim();
if (id1 == "Passport")
return PartialView("~/Views/HomePage/id1.cshtml", ps);
else
return PartialView("~/Views/HomePage/pancard.cshtml", pn);
}
[HttpPost]
public ActionResult RacersByCountryPartial(string id,Passport ps, Pan pn,DetailsEntry dt)
{
return View(id);
}
}
}
this is my Mainview.
@model MvcApplication2.DetailsEntry
@{
ViewBag.Title = "Submit";
}
<h2>Submit</h2>
@using (Html.BeginForm("Submit", "HomePage", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>DetailsEntry</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ClientId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ClientId)
@Html.ValidationMessageFor(model => model.ClientId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ClientName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ClientName)
@Html.ValidationMessageFor(model => model.ClientName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeId)
@Html.ValidationMessageFor(model => model.EmployeeId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmpCitizenId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpCitizenId)
@Html.ValidationMessageFor(model => model.EmpCitizenId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmpName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpName)
@Html.ValidationMessageFor(model => model.EmpName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Nationality)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Nationality)
@Html.ValidationMessageFor(model => model.Nationality)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DocumentId)
</div>
<div class="editor-field">
@Html.DropDownListFor(Model => Model.DocumentId, @ViewBag.docid as SelectList,"Select document Type")
@Html.ValidationMessageFor(Model=>Model.DocumentId)
</div>
</fieldset>
}
<div id="container"> </div>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="//code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#DocumentId").change(function () {
$("#log").ajaxError(function (event, jqxhr, settings, exception) {
alert(exception);
});
var countrySelected = $("select option:selected").first().text();
$.get('@Url.Action("RacersByCountryPartial")',
{ id: countrySelected }, function (data) {
$("#container").html(data);
});
});
});
</script>
This is my partial view.
@model MvcApplication2.Passport
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Passport</legend>
<div class="editor-label">
@Html.LabelFor(model => model.pissueddate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.pissueddate)
@Html.ValidationMessageFor(model => model.pissueddate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.pissuedlocation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.pissuedlocation)
@Html.ValidationMessageFor(model => model.pissuedlocation)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.pimage)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.pimage)
@Html.ValidationMessageFor(model => model.pimage)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
There is one submit button at the end of partial view. I want to save both main view and partial view data on single button click to respective tables. Please advise me.