Asp.net mvc 如何在控制器中获取partialview数据
我在一个视图上使用了三个Asp.net mvc 如何在控制器中获取partialview数据,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc 3,我在一个视图上使用了三个partialview,我有一个提交按钮,点击它我想将信息发送到数据库,我必须从所有partialview中检索数据 你能给我提供做这件事的正确信息吗 Darin我使用L2S,所以当我拖动我的存储过程时,我在 [global::System.Data.Linq.Mapping.FunctionAttribute(Name="SP_Name")] public int SP_Name( [global
partialview
,我有一个提交按钮,点击它我想将信息发送到数据库,我必须从所有partialview
中检索数据
你能给我提供做这件事的正确信息吗
Darin我使用L2S,所以当我拖动我的存储过程时,我在
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="SP_Name")]
public int SP_Name(
[global::System.Data.Linq.Mapping.ParameterAttribute(Name="EmployeeID", DbType="Int")] System.Nullable<int> EmployeeID
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), EmployeeID);
encounterID = ((System.Nullable<int>)(result.GetParameterValue(293)));
return ((int)(result.ReturnValue));
}
}
当我返回这个文件时,我收到了一个文件,它要求我保存它。
我已经使用flidder
进行了检查,在URL中,它显示路径仅为/
其中,如果我填充任何特定的partialview
,它会显示类似/Controller Name/partialview
您能帮我解决这个问题吗?嗯,向控制器操作发送数据通常是通过向该控制器操作执行HTTP请求来完成的。执行HTTP请求的方式有多种:
标记
包装所有部分,其中包含多个提交按钮(具有不同的名称)。然后,当您单击一个提交按钮时,所有输入字段都将发送到控制器操作,然后在控制器操作中,您可以根据单击提交按钮的位置处理数据
如果使用第二个选项,那么只需通过单击按钮获取需要发送的值,并随AJAX请求发送
更新: 根据评论部分的要求,这里介绍了如何将第一种技术付诸实施。它使用两个部分而不是三个部分,但可以很容易地进行推断 与往常一样,首先定义一个视图模型,该模型将表示您希望在此特定视图上使用的数据:
public class MyViewModel
{
public Partial1ViewModel Model1 { get; set; }
public Partial2ViewModel Model2 { get; set; }
}
public class Partial1ViewModel
{
public string Foo { get; set; }
}
public class Partial2ViewModel
{
public string Bar { get; set; }
}
然后控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
Model1 = new Partial1ViewModel { Foo = "foo" },
Model2 = new Partial2ViewModel { Bar = "bar" },
};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// Here you have access to model.Model1.Foo and model.Model2.Bar =>
var button = "";
if (!string.IsNullOrEmpty(Request["submit1"]))
{
// submit1 button was used
button = "submit1";
}
else if (!string.IsNullOrEmpty(Request["submit2"]))
{
// submit2 button was used
button = "submit2";
}
var result = string.Format("thanks for submitting using {0}", button);
return Content(result, "text/plain");
}
}
然后是主视图(~/Views/Home/Index.cshtml
):
和两个相应的编辑器模板(或部分,如果您愿意):
~/Views/Home/EditorTemplates/Partial1ViewModel.cshtml
:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.Model1)
@Html.EditorFor(x => x.Model2)
}
@model Partial1ViewModel
<h2>Partial 1</h2>
<div>
@Html.LabelFor(x => x.Foo)
@Html.EditorFor(x => x.Foo)
<input type="submit" value="Submit me!" name="submit1" />
</div>
@model Partial2ViewModel
<h2>Partial 2</h2>
<div>
@Html.LabelFor(x => x.Bar)
@Html.EditorFor(x => x.Bar)
<input type="submit" value="Submit me!" name="submit2" />
</div>
你能举例说明如何实施吗this@Evostract,您对这两种技术中的哪一种感兴趣?请为我提供第一种方法method@Evostract,请查看我的更新。Darin我正在使用L2S,所以当我拖动存储过程时,我在
@model Partial1ViewModel
<h2>Partial 1</h2>
<div>
@Html.LabelFor(x => x.Foo)
@Html.EditorFor(x => x.Foo)
<input type="submit" value="Submit me!" name="submit1" />
</div>
@model Partial2ViewModel
<h2>Partial 2</h2>
<div>
@Html.LabelFor(x => x.Bar)
@Html.EditorFor(x => x.Bar)
<input type="submit" value="Submit me!" name="submit2" />
</div>