模型绑定和 GET 请求?

发布于 2024-07-16 20:40:51 字数 990 浏览 5 评论 0原文

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", "Index", 
         new { str = Model.Str, chr = Model.Chr, dbl = Model.Dbl })%>
</div>

模型绑定基础设施可以这样做吗? 如果是这样,需要对我的样品进行哪些操作才能使其正常工作?

There are tons of examples for model binding in html forms, but I'm wondering if its possible to, and if so how to, use model binding for ActionLinks/GET requests.

So, given the following model

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

and the following route (I'm not sure how this would be formed; I present it to show how I'd like the URL presents the properties Str, Chr and Dbl)

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

I'd like to use it this way in my Controller

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

and this way in my page (two possible options; are there more?)

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

Is this possible with the model binding infrastructure? And if so, what needs to be done to my samples to get them working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2024-07-23 20:40:51

我认为您必须选择类作为参数方法

[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 将映射到您的路线路径中的什么参数? 我很确定那里一定存在一对一的相关性。

I think you'll have to pick either the class as a parameter approach

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

or the properties as parameters approach

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

...though in the class as a parameter approach, you can use the "UpdateModel" method. You can pass in a whitelist of parameters you want to update with that method just in case you only want to update a few values in your model.

Also, In your MapRoute, what parameter will lurl map to in your route path? I'm pretty sure there has to be a one to one correlation there.

将军与妓 2024-07-23 20:40:51

您还可以使用 自定义模型绑定器。 另请阅读此内容

You can also use a custom model binder. Also read this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文