Home.cshtml
@model Models.MyData
@using (Html.BeginForm("AddData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
HomeController.cs
[HttpPost]
public async Task<ActionResult> AddData(MyData mydata)
{
return View("AddDetails", mydata);
}
我现在在mydata
处获取值,我想重定向到具有与新重定向页面的页面加载相同的模型值的下一页,我编写了ajax调用,但它传递了空模型。
AddDetails.cshtml
@model Models.MyData
<script>
var someRootPath = "@Url.Content("~")";
$.ajax({
url: someRootPath + 'Home/Process',
data: $('#AddData').serialize(),
type: "POST",
});
}
</script>
HomeController.cs
[HttpPost]
public async Task Progess(MyData mydata)
{
如何从上一页获取mydata模型?我想说明为什么这就是为什么首先使用return view
而不是RedirectToAction
答案 0 :(得分:1)
https://dotnetfiddle.net/c7hAFy
控制器
namespace Testy20161006.Controllers
{
public class MyData
{
public string NeoData { get; set; }
}
public class HomeController : Controller
{
[HttpPost]
//adding generic ActionResult to Task type
public async Task<ActionResult> Progess(MyData mydata)
{
return View();
}
[HttpPost]
public async Task<ActionResult> AddData(MyData mydata)
{
return View("AddDetails", mydata);
}
public ActionResult Tut137()
{
MyData myData = new MyData { NeoData = "neoData" };
return View(myData);
}
首页-在我的情况下为索引或Tut137-
@model Testy20161006.Controllers.MyData
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Tut137</title>
</head>
<body>
@using (Html.BeginForm("AddData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(r=>r.NeoData);
<input id="theButton" type="submit" value="Go" />
}
</body>
</html>
AddDetails.cshtml
@model Testy20161006.Controllers.MyData
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AddDetails</title>
<script src="~/Scripts/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
var someRootPath = "@Url.Content("~")";
$.ajax({
//rename Process to Progess to match call to method called
url: someRootPath + 'Home/Progess',
data: $('#AddData').serialize(),
type: "POST",
});
})
</script>
</head>
<body>
<div>
@Html.TextBoxFor(r => r.NeoData, new { id = "AddData" })
</div>
</body>
</html>