简体   繁体   English

asp.net mvc路由使用表单get具有2个动作和相同的视图

[英]asp.net mvc route using form get with 2 action and same view

I have url problem when doing search on page. 在页面上搜索时出现网址问题。 So, I have Index View with a year textbox and a search button inside a form using GET method 所以,我有一个带有年份文本框的索引视图和一个使用GET方法的表单内的搜索按钮

Index.cshtml Index.cshtml

@using (Html.BeginForm("Search", "Service", new { Year = Model.Year }, FormMethod.Get))
{
    <p>
        <div class="form-inline">
            @Html.EditorFor(model => model.Year, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Year" } })
            <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search</button>
        </div>
    </p>
}

I put Search as actionName in BeginForm because when redirect to Service/Index the service data should not be loaded at the first time. 我将Search作为actionName放在BeginForm因为当重定向到Service / Index时,不应在第一次加载服务数据。 So, I'm using another Action, "Search" to handle this request, if the user doesn't enter the year then it will load all of the data, but if the user enter the year it will load the data based on the year. 因此,我正在使用另一个操作“搜索”来处理此请求,如果用户未输入年份,则它将加载所有数据,但是如果用户输入年份,则它将基于年。

Here is the controller to handle the request 这是处理请求的控制器

ServiceController.cs ServiceController.cs

public ActionResult Index()
{
    var vm = new ServiceIndexViewModel();
    return View(vm);
}

public async Task<ActionResult> Search(int? year)
{
    var vm = new ServiceIndexViewModel();

    if (ModelState.IsValid)
    {
        var list = await service.Search(year);
        vm.Services = AutoMapper.Mapper.Map<IEnumerable<ServiceListViewModel>>(list);
    }

    return View("Index", vm);
}

and the custom route to handle the routing 和用于处理路由的自定义路由

RouteConfig.cs RouteConfig.cs

routes.MapRoute(
    "ServiceSearch",
    "Service/Search/{Year}",
    new { controller = "Service", action = "Search", Year = UrlParameter.Optional }
);

// default route
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Company", action = "Index", id = UrlParameter.Optional }

); );

but I've got the url like this: 但我有这样的网址:

http://localhost:18132/Service/Search?Year= or http://localhost:18132/Service/Search?Year=2017 http:// localhost:18132 / Service / Search?Year =http:// localhost:18132 / Service / Search?Year = 2017

and I want the url to be displayed like this 我希望网址显示为

http://localhost:18132/Service/Search or http://localhost:18132/Service/Search/Year/2017 http:// localhost:18132 / Service / Searchhttp:// localhost:18132 / Service / Search / Year / 2017

What's wrong with my routing? 我的路线有什么问题? How to solve this? 如何解决呢?

First of all your route should be defined like this: 首先,您的路线应如下定义:

routes.MapRoute(
    "ServiceSearch",
    "Service/Search/Year/{Year}",
    new { controller = "Service", action = "Search", Year = UrlParameter.Optional });

But your problem is something else your code is not falling to this defined route and it falls to the default route. 但是您的问题是您的代码没有落入此定义的路由,而是落入了默认路由。 Make sure that the default route is bellow this route if it's still not working comment me here and I will tell you what I have in my mind. 如果默认路由仍无法使用,请确保该路由位于此处,如果在此处评论我,我将告诉您我的想法。

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

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