我有一个局部的观点,可以从我的网站上的任何地方调用。当用户在submit中单击Partial View按钮时,就会发送电子邮件。用户不需要任何信息,因为这已经包含在我的Model中。C#代码在我的Partial View中,而不是我的Controller中,因为我需要将Model (包含几个arrays)作为参数传递,但是没有成功。当用户单击按钮时,表单提交并发送邮件,然后用户被重定向到Partial View的地址,而不是显示Partial View的原始View的地址。我怎样才能防止这种重定向?
例如,如果PartialView.cshtml是从/Home/View/加载的,那么我将在post中重定向到/Home/PartialView。
PartialView.cshtml通过JQuery中的Ajax post从HomeController.cs加载
[HttpPost]
public ActionResult PartialView()
{
....create myModel....
return PartialView(myModel);
}PartialView.cshtml
@using (Html.BeginForm()) {
<input type="submit" value="email" />
}
...rest of partial view...
@{
if (IsPost){
....sends email using data in model....
}
}以前I有以下内容,但是控制器接收到的模型中的数据始终为null。(不是问题的一部分,但如果有人问为什么我的代码不在我的控制器中):
PartialView.cshtml
@using (Html.BeginForm("sendMail", "Home", FormMethod.Post, new { myModel = Model }))
{
@Html.HiddenFor(a => a.firstArray);
@Html.HiddenFor(a => a.secondArray);
<input type="submit" value="email" />
}HomeController.cs
[HttpPost]
public void sendMail(myModelType myModel )
{
....send email using data in model....
}发布于 2015-08-06 14:59:48
您正在返回部分视图,因此它当然会将您重定向到部分视图:
return PartialView(myModel);相反,您希望获得父服务器,您可以通过ParentActionViewContext获得它。试一试:
var controller = ControllerContext.ParentActionViewContext.RouteData.Values["Controller"] as string;
var action = ControllerContext.ParentActionViewContext.RouteData.Values["Action"] as string;
return View(action, controller);https://stackoverflow.com/questions/31858863
复制相似问题