问题描述
亲爱的朋友们,
我设计了2个局部视图,1。文本框2.网格。我需要从主页面保存两个局部视图值。
我已经给出了文本框和网格的单独代码,我需要将这些代码混合在一起。
我尝试过:
此代码用于保存网格值:
[HttpPost]
public ActionResult Create(List< mrdtl> ci)
{
if(ModelState.IsValid)
{
使用(TelecomContext dc = new TelecomContext())
{
foreach(var i in ci)
{
dc.MRDtl.Add(i);
}
dc.SaveChanges();
ViewBag.Message =数据已成功保存!;
ModelState.Clear();
ci = new List< mrdtl> {new MRDtl {IRateCode =,ItemCode =,QtyRequested = 0}};
}
}
返回查看(ci );
}
此代码用于保存文本框值,例如。像这样:
[HttpPost]
公共ActionResult创建(ItemMaster itemmaster)
{
if(ModelState.IsValid)
{
itemmaster.MRNo =MR-001;
itemmaster.MRDate = DateTime 。现在;
db.ItemMaster.Add(itemmaster);
db.SaveChanges();
return View(new ItemMaster());
}
else {ViewBag.ErrMsg =输入必填字段; }
返回View(itemmaster);
}
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);
}
推荐答案
@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);
}
这里唯一的问题是HTML元素的name属性必须对应于对象上的属性开始传递给你的控制器(在这种情况下你的ItemMaster类)。因此,只要两个部分中的编辑是以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
<!-- This would be your first partial cshtml file -->
@Html.TextBox("ItemName")
<!-- This would be in your second partial cshtml file -->
@Html.TextBox("ItemValue")
然后你的ItemMaster类需要看起来像
Then your ItemMaster class would need to look like
public class ItemMaster
{
public string ItemName {get;set;}
public double ItemValue {get;set;}
}
在Ajax.BeginForm中使用的示例javascript函数
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");
}
};
这篇关于如何在MVC中从主视图中将两个局部视图值保存到db中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!