简体   繁体   English

RedirectToAction ASP .NET MVC

[英]RedirectToAction ASP .NET MVC

I currently read Freeman's book ASP .NET MVC 4 and do a web application.我目前正在阅读 Freeman 的书 ASP .NET MVC 4 并做了一个 Web 应用程序。 So, there is a controller named "Cart" with method "AddToCart" and a View.因此,有一个名为“Cart”的控制器,带有方法“AddToCart”和一个视图。 In the view we have such code:在视图中我们有这样的代码:

@model SportsStore.Domain.Entities.Product

<div class="item">
    <h3>@Model.Name</h3>
    @Model.Description
    @using (Html.BeginForm("AddToCart", "Cart"))
    {
        @Html.HiddenFor(x => x.ProductID)
        @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
        <input type="submit" value="+ Add to cart" />
    }

    <h4>@Model.Price.ToString("c")</h4>
</div>

and the method's of the controller code:和控制器代码的方法:

public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
{
   Product product = repository.Products
      .FirstOrDefault(p => p.ProductID == productId);
   if (product != null)
   {
      cart.AddItem(product, 1);
   }
   return RedirectToAction("Index", new { returnUrl });
}

The code compiles very good, but I have here some questions.代码编译得很好,但我在这里有一些问题。

1) If you put a breakpoint on the line: 1)如果你在行上放置一个断点:

return RedirectToAction("Index", new { returnUrl });

you'll see that returnUrl has value " / ".你会看到 returnUrl 的值为“ / ”。 How?如何? Where did he get it?他从哪里得到的?

2) What are these lines of code doing, because there is no information in the book about it. 2)这几行代码在做什么,因为书中没有关于它的信息。

@Html.HiddenFor(x => x.ProductID)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)

Let me answer Your second question first.我先回答你的第二个问题。 @Html has a set of helpers generating html. @Html有一组生成 html 的助手。

@Html.HiddenFor(x => x.ProductID)

will output something like: <input type="hidden" name="ProductID" value="1" />将输出类似: <input type="hidden" name="ProductID" value="1" />

@Html.Hidden("returnUrl", Request.Url.PathAndQuery)

will output something like: <input type="hidden" name="returnUrl" value"/" />将输出类似: <input type="hidden" name="returnUrl" value"/" />

Now look at AddToCart method signature:现在看看 AddToCart 方法签名:

AddToCart(Cart cart, int productId, string returnUrl)

Values of these hidden inputs but will be included in the form while posting to AddToCart action and model binding will assign them to productId and returnUrl arguments accordingly.这些隐藏输入的值将包含在表单中,同时发布到 AddToCart 操作和模型绑定将相应地将它们分配给productIdreturnUrl参数。

Next to answer first question:接下来回答第一个问题:

Request.Url.PathAndQuery property returns absolute path of request with query parameters. Request.Url.PathAndQuery属性返回带有查询参数的请求的绝对路径。 So if you came to AddToCart view from Index view then returnUrl will be "/", because Your Index view is a root of Your webapp, so to speak.因此,如果您从 Index 视图来到 AddToCart 视图,则returnUrl将是“/”,因为您的 Index 视图是您的 webapp 的根,可以这么说。 It all comes down how routing is configured in Your application.这一切都取决于如何在您的应用程序中配置路由。

For more information go watch these tutorials: Controllers and Routing有关更多信息,请观看这​​些教程:控制器和路由

Index is a name that MVC "assumes" so if you go to the index view you won't see索引是MVC“假定”的名称,因此如果您转到索引视图,您将看不到

/index 

in the url.在网址中。 just /只是 /

the 2 hidden fields that you showed are an MVC was of creating您展示的 2 个隐藏字段是一个 MVC 创建的

<input type="hidden" ...

if is a way to store information that you need on your view for passing back to the controller through a post back or ajax call if 是一种在视图中存储所需信息的方法,以便通过回发或 ajax 调用传回控制器

There is the parameter used by your method controller, the hidden fields is the input fied used to send the paramenters to your server side script您的方法控制器使用了参数,隐藏字段是用于将参数发送到服务器端脚本的输入

"returnUrl", Request.Url.PathAndQuery

Request.Url.PathAndQuery请求.Url.PathAndQuery

will be used by RedirectToAction to establish where to send you RedirectToAction 将使用它来确定将您发送到何处

return RedirectToAction("Index", new { returnUrl }); 

p.ProductID p.ProductID

to manage what you want add to chart管理您要添加到图表的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM