Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends,

I have designed 2 partial views, 1. Textboxes 2. grid. I need to save two partial view values from main page.

I have given seprate code for textbox and grid, i need to mingle these code.

What I have tried:

This code for save grid values:
[HttpPost]
public ActionResult Create(List<mrdtl> ci)
{
if (ModelState.IsValid)
{
using (TelecomContext dc = new TelecomContext())
{
foreach (var i in ci)
{
dc.MRDtl.Add(i);
}
dc.SaveChanges();
ViewBag.Message = "Data successfully saved!";
ModelState.Clear();
ci = new List<mrdtl> { new MRDtl { IRateCode = "", ItemCode = "", QtyRequested = 0 } };
}
}
return View(ci);
}

This code for save textbox values, For Eg. like this:

[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);
}
Posted
Updated 27-Jul-16 9:30am

1 solution

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

HTML
 @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

C#
[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
<!-- This would be your first partial cshtml file -->
@Html.TextBox("ItemName")


<!-- This would be in your second partial cshtml file -->

@Html.TextBox("ItemValue")


Then your ItemMaster class would need to look like

C#
public class ItemMaster 
{
    public string ItemName {get;set;}
    public double ItemValue {get;set;}
}


Example javascript function to be used in Ajax.BeginForm

JavaScript
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");
}
};
   
v3
Comments
Vivek.anand34 29-Jul-16 0:33am
   
What Function is this: OnBegin = "onBeginFunctionHere",
OnSuccess = "onSuccessFunctionHere",
OnFailure = "onFailureFunctionHere",
OnComplete = "onCompleteFunctionHere"
David_Wimbley 29-Jul-16 2:35am
   
Those are javascript functions that you need to create. In those javascript functions is where you would handle any error messages or alerts based on the success/failure of the action that has been taken.

I've edited my answer to show what i mean

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900