Im using the Ajax form html helper, can easily swap it for the html form html helper.
But for the HTML you would do something like this
@using (Ajax.BeginForm("Create", "ControllName", null, new AjaxOptions()
{
OnBegin = "Form.onBeginFunctionHere",
OnSuccess = "Form.onSuccessFunctionHere",
OnFailure = "Form.onFailureFunctionHere",
OnComplete = "Form.onCompleteFunctionHere"
}))
{
@Html.Partial("/Your/First/Partial/Here.cshtml", PassModelInIfYouHaveOneHere)
@Html.Partial("/Your/Second/Partial/Here.cshtml", PassModelInIfYouHaveOneHere)
<input type="submit" value="Save"/>
}
Then for your server side code you can use your create action
[HttpPost]
public ActionResult Create(ItemMaster itemmaster)
{
if (ModelState.IsValid)
{
itemmaster.MRNo= "MR-001";
itemmaster.MRDate = DateTime.Now;
db.ItemMaster.Add(itemmaster);
db.SaveChanges();
return View(new ItemMaster());
}
else { ViewBag.ErrMsg = "Enter Mandatory Fields"; }
return View(itemmaster);
}
The only gotcha here is that the name attribute of your HTML elements has to correspond to a property on the object begin passed to your controller (in this case your ItemMaster class). So as long as the edits in your two partials are property named after properties in ItemMaster. You should pass this along.
To explain what i mean
<!--
@Html.TextBox("ItemName")
<!--
@Html.TextBox("ItemValue")
Then your ItemMaster class would need to look like
public class ItemMaster
{
public string ItemName {get;set;}
public double ItemValue {get;set;}
}
Example javascript function to be used in Ajax.BeginForm
var Form = {
onBeginFunctionHere = {
alert("This is prior to submitting to server side code. This is where you might pop up a loading spinner to show that your UI is busy");
},
onSuccessFunctionHere = {
alert("You may pass back something to indicate a failure (or use http code) but regardless, your call to the server side code was successful");
},
onCompleteFunctionHere = {
alert("regardless of success/failure, the server side ajax call is complete");
},
onFailureFunctionHere = {
alert("Something in your form submission failed");
}
};