让我们提供 2 种通过简单示例展示的方法:
最快的方法是通过查询字符串发送。像这样:
public ActionResult Index()
{
TempData["message"] = "Hi";
return View();
}
[HttpGet]
public ActionResult About(string str1, string str2) { . . . }
然后,在您的索引视图中:
@{
string str = TempData["message"].ToString();
@Html.ActionLink("Go to About View", "About", new { str1 = @str, str2="some text"})
}
另一方面,您可以将数据发布到控制器,而不是通过查询字符串发送;
假设我们在这里有一个简单的视图模型:
public class DoubleStr_ViewModel
{
public string str1 { get; set; }
public string str2 { get; set; }
}
然后在控制器内部:
public ActionResult About()
{
TempData["message"] = "Hi";
return View(new DoubliStr_ViewModel());
}
[HttpPost]
public ActionResult About(DoubleStr_ViewModel input)
{
return View();
}
现在,在 About 视图中:
@using Your.Path.To.ViewModels
@model DoubleStr_ViewModel
<input type="hidden" value="@TempData["message"].ToString()" id="tmpHidden" />
<form method="post">
@Html.HiddenFor(x => x.str1)
@Html.TextBoxFor(x => x.str2)
<button type="submit">SUBMIT</button>
</form>
@section scripts{
<script>
$('form').submit(function () {
$('input#str1').val($('input#tmpHidden').val());
});
</script>
}
在您的情况下,要触发表单提交,您将拥有:
$('#btnContinue').click(function () {
// first, fill the hidden value(s) into your model
$('input#str1').val($('input#tmpHidden').val());
var createFormName = $("#createFormName");
createFormName.submit();
}
希望,这会有所帮助。