加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_开封站长网 (http://www.0378zz.com/)- 科技、AI行业应用、媒体智能、低代码、办公协同!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

ASP.NET MVC - 表单的动态操作和参数

发布时间:2023-06-13 09:32:24 所属栏目:Asp教程 来源:网络
导读: 我正在研究一些MVC,我需要动态地将表单路由到某个动作和参数组合。到目前为止,我有这个:
PageViewModel
{
public string Action {get;set;}
public string Parameter {get;set;}

我正在研究一些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)%>
    
<%}%>
<%-- ... --%>

这很有效ASP表单,但它有很多字符串可以告诉它去哪里。

我想要的是一种在视图中定义表单参数的类型安全方法,但我对如何实现这一点感到有点迷茫。也许看起来像这样 -

<% using (Html.BeginForm(Model.??ExpressionToGetAction??)) {%>
    <%=Html.TextBox(Model.??ExpressionToGetParameter??)%>
    
<%}%>

或者,是否有办法获取用于生成此视图的操作和参数,可能来自路径数据?

或者是否应该有自动处理所有这些的自定义路由方案?

所以,我真正想要的是实现这一目标的最优雅和类型安全的方法。谢谢!

修改

正如Josh所指出的那样,表格将提交回行动。这有点修改了代码:

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)%>
    
<%}%>
<%-- ... --%>

目前还不清楚如何让文本框按名称将参数绑定回创建视图的操作,而不是明确指定它。

(编辑:开发网_开封站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章