Asp.net将两个参数路由到控制器中的方法

时间:2017-08-12 21:13:31

标签: asp.net razor asp.net-ajax

我有以下功能

[Route("/ClientReservations/{id}/{name}")]
        public ActionResult GetView(int id,string name)
        {
            ReservationHouseDetails houseDetails=null;
            repository.GetHousesForReservation(id).Where(item => item.Name.Equals(name)).ForEach(item => houseDetails = new ReservationHouseDetails()
            {
                House=item,
                Meal = repository.GetHouseMealForReservation(item.Id),
                Participants = repository.GetParticipantsHouseForReservation(item.Id)
            });

            return PartialView("ReservationHouseDetails", houseDetails);
        }

以这种方式调用此方法

@using (Ajax.BeginForm(new AjaxOptions
    {
        Url = Url.Action("GetView", "ClientReservations", new { id = Model.Reservation.Id, name = Model.SelectedHouseDetailsText }),
        HttpMethod = "get",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "divForSelectHouse"
    }))
    {
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="SelectHouse"
                       value="Wybierz domek" />
            </div>
        </div>

    }

我已经检查了debbug模式,并且值名称是从模型中分配的值。然而,当它传递给提出的方法第二个参数&#34; name&#34;一片空白。可能是什么原因?

1 个答案:

答案 0 :(得分:0)

Url.Action("GetView", "ClientReservations", new { id = Model.Reservation.Id, name = Model.SelectedHouseDetailsText })

中的问题

您的Url.Action会生成/ClientReservations/{id}?name={name}

您必须在"Default"中的RouteConfig.cs之前添加路线:

routes.MapRoute(
    name: "GetView",
    url: "{controller}/{action}/{id}/{name}",
    defaults: new { controller = "ClientReservations", action = "GetView", id = UrlParameter.Optional, name = UrlParameter.Optional }
);

WebApiConfig.cs

config.Routes.MapHttpRoute(
    name: "GetViewApi",
    routeTemplate: "{controller}/{id}/{name}",
    defaults: new { controller = "ClientReservations", action = "GetView", id = RouteParameter.Optional, name = RouteParameter.Optional }
);