1
我正在处理一些MVC,我需要将表单动态地路由到某个操作和参数组合。到目前为止,我有这样的:ASP.NET MVC - 表单的动态操作和参数
PageViewModel
{
public string Action {get;set;}
public string Parameter {get;set;}
/*... other properties for the form */
}
PageController
{
public ViewResult MyAction(string myParamterName) {
return View("CommonView",
new PageViewModel{Action="MyAction", Parameter="myParameterName"));
}
public ViewResult YourAction(string yourParamterName) {
return View("CommonView",
new PageViewModel{Action="YourAction", Parameter="yourParameterName"));
}
/* ... and about 15 more of these */
}
CommonView.aspx:
<%-- ... --%>
<% using (Html.BeginForm(Model.Action,"PageController",FormMethod.Get)) {%>
<%=Html.TextBox(Model.Parameter)%>
<input id="submit" type="submit" value="Submit" />
<%}%>
<%-- ... --%>
这工作,但它得到了很多串漂浮的告诉它到哪里去。
我想要的是一种在视图内定义窗体参数的类型安全方法,但我在如何实现这一点方面有点失落。也许,一些看起来像这样 -
<% using (Html.BeginForm<PageController>(Model.??ExpressionToGetAction??)) {%>
<%=Html.TextBox(Model.??ExpressionToGetParameter??)%>
<input id="submit" type="submit" value="Submit" />
<%}%>
或者,有没有办法从路由数据获得用于生成这种观点的动作和参数,也许?
还是应该有一个自定义的路由方案,可以处理所有这些自动?
所以,我真正想要的是最优雅和类型安全的方式来实现这一点。谢谢!
编辑
由于乔希指出,表单将提交回动作。这个有点修剪代码:
PageViewModel
{
public string ParameterName {get;set;}
/*... other properties for the form */
}
PageController
{
public ViewResult MyAction(string myParamterName) {
return View("CommonView",
new PageViewModel{ParameterName ="myParameterName"));
}
public ViewResult YourAction(string yourParamterName) {
return View("CommonView",
new PageViewModel{ParameterName ="yourParameterName"));
}
/* ... and about 15 more of these */
}
CommonView.aspx:
<%-- ... --%>
<% using (Html.BeginForm(FormMethod.Get)) {%>
<%=Html.TextBox(Model.ParameterName)%>
<input id="submit" type="submit" value="Submit" />
<%}%>
<%-- ... --%>
现在还不清楚如何让文本框按名称参数绑定回从该观点不明确创建行动指定它。
啊,很高兴知道。有没有办法获得动作的参数名称,或者其他方式将文本框绑定到参数? – 2009-11-25 16:24:28
您在返回视图的操作中定义的模型是THE Model,所以我认为不需要做任何特殊的事情。 – 2009-11-25 16:36:10