加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 模型绑定和GET请求?

发布时间:2020-12-15 19:30:37 所属栏目:asp.Net 来源:网络整理
导读:有许多html表单中的模型绑定示例,但我想知道它是否可能,如果是这样,使用ActionLinks / GET请求的模型绑定. 所以,给出以下模型 public class Lurl{ public string Str {get;set;} public char Chr {get;set;} public double Dbl {get;set;}} 和以下路由(我不
有许多html表单中的模型绑定示例,但我想知道它是否可能,如果是这样,使用ActionLinks / GET请求的模型绑定.

所以,给出以下模型

public class Lurl
{
  public string Str {get;set;}
  public char Chr {get;set;}
  public double Dbl {get;set;}
}

和以下路由(我不知道这将如何形成;我提出来显示我想要的URL如何呈现属性Str,Chr和Dbl)

routes.MapRoute(
    "LurlRoute","Main/Index/{str}/{chr}/{dbl}",new
    {
        controller = "Main",action = "Index",lurl = (Lurl)null
    }
);

我想在我的控制器中使用它

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index(Lurl lurl)
{
  /* snip */
}

这样在我的页面(两个可能的选择;有更多的?)

<div class="links">
  <%Html.ActionLink("Link one","Index",new { lurl = Model })%><br />
  <%Html.ActionLink("Link two",new { str = Model.Str,chr = Model.Chr,dbl = Model.Dbl })%>
</div>

模型绑定基础设施是否可能?如果是这样,我的样品需要做些什么才能让他们工作?

解决方法

我想你必须选择该类作为参数方法
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index(Lurl lurl)
{
   /* snip */
}

或属性作为参数方法

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index(string str,char chr,double dbl)
{
    /* snip */
}

…虽然在类中作为参数方法,可以使用“UpdateModel”方法.您可以使用该方法传递要更新的参数白名单,以防您只想更新模型中的几个值.

另外,在您的MapRoute中,路由路径中的lurl映射到哪个参数?我确定在那里必须有一到一个关联.

(编辑:李大同)

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

    推荐文章
      热点阅读