简体   繁体   English

ASP MVC路由:相同操作/视图的3条路由

[英]ASP MVC routes: 3 routes for the same Action/View

I have this controler with only 1 action and working with id(int): 我只有1个动作并使用id(int)使用此控制器:

public class PropertiesController: Controller
{
    public ActionResult Index (int idEstate = 0, int idCity = 0, int [] idNeighborhoods = null)
    {
        string IdEstate ;
        string IdCity ;
        int [] IdNeighborhoods ;

        #ReadOrSetCookie

        #SetViewBags

        #ReturnNeighborhoods

        #ReturnProperties

        return View();
    }
}

In the View I have a form, the user can select 1 state and 1 city and a set of neighborhoods. 在“视图我有一个表单”中,用户可以选择1个州和1个城市以及一组社区。 And when submitted returns to the Action Index that instead of reading this data a cookie will receive from the form, then search and return the properties. 提交后,返回到操作索引,而不是读取此数据,cookie将从表单中接收,然后搜索并返回属性。 Working fine. 工作正常。 I no have problens here. 我这里没有问题。

My problem is on return displays for user the following url everytime: 我的问题是每次都会在返回显示中为用户显示以下网址:

MySite/properties/index MySite /属性/索引

And I need to return depending on what the user selects in the form If he just reported the state: 我需要根据用户在表单中选择的内容返回,如果他只是报告了状态:

MySite/properties/{StateInformed} MySite / properties / {StateInformed}

If he also selected a city: 如果他还选择了一个城市:

MySite/properties/{StateInformed}/{CityInformed} MySite / properties / {StateInformed} / {CityInformed}

eg www.mysite.com/properties/Florida/Miami 例如www.mysite.com/properties/Florida/Miami

My routConfig is the default: 我的routConfig是默认值:

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

And my simplified form is (I'm not reporting verb or anything different) 我的简化形式是(我没有报告动词或其他任何东西)

@using (Html.BeginForm("Index", "Home"))
{
   @Html.DropDownList("idEstado", new SelectList(ViewBag.ListaEstados, "idEstado", "txtNomeEstado", ViewBag.idEstadoCookie))
   @Html.DropDownList("idCidade", new SelectList(ViewBag.ListaCidades, "idCidade", "txtNomeCidade", ViewBag.idCidadeCookie))
   <input type="submit" value="BUSCAR" />
}

Is possible do this with only 1 action and working with id (int)? 仅用1个动作并使用id(int)就能做到吗? Could someone help me do this? 有人可以帮我吗?

Update here ----------------------------------------- 在这里更新-----------------------------------------

Using the suggested here I try this... 使用这里的建议我尝试一下...

I add this decoration in the controller: 我将这种装饰添加到控制器中:

    [Route("")]
    [Route("imoveis/{idEstado?}")]
    [Route("imoveis/{idEstado?}/{idCidade?}")]
    [Route("imoveis/{idEstado?}/{idCidade?}/{idRegiao?}")]
    public ActionResult Index (int idEstate = 0, int idCity = 0, int [] idNeighborhoods = null)

I change the form in view to using get and have a Id: 我将表单更改为使用get和id:

@using (Html.BeginForm("Index", "imoveis", FormMethod.Get, new { id = "buscaimovel" }))

I add for each selct list in form the html atribut form. 我为每个选择列表以html属性形式添加。 But in the select to State I put form = "buscaimovel" and for others I put a diferente form like: 但是在选择状态时,我输入了form =“ buscaimovel”,对于其他人,我输入了不同的形式,例如:

@Html.DropDownList("idEstado", new SelectList(ViewBag.ListaEstados, "idEstado", "txtNomeEstado", ViewBag.idEstadoCookie), new { @form = "buscaimovel" })
@Html.DropDownList("idCidade", new SelectList(ViewBag.ListaCidades, "idCidade", "txtNomeCidade", ViewBag.idCidadeCookie), new { @form = "nulo" })

And finally I use a jquery to change the form attribute when an item is selected 最后,当选择一个项目时,我使用jQuery来更改表单属性

$(document).ready(function () {
        $("#idRegiao")
            .change(function () {
                if ($("#idRegiao").val() != null) $("#idRegiao").attr("form", "buscaimovel");
            });
    });

Nice! 真好! Now I have my diferent url return's: 现在我有不同的网址返回的:

//localhost:57094/?idEstado=11 // localhost:57094 /?idEstado = 11

//localhost:57094/?idEstado=11&idCidade=41 // localhost:57094 /?idEstado = 11&idCidade = 41

//localhost:57094/?idEstado=11&idCidade=41&idRegiao=258 // localhost:57094 /?idEstado = 11&idCidade = 41&idRegiao = 258

Or this: 或这个:

//localhost:57094/imoveis/11 //本地主机:57094 / imoveis / 11

//localhost:57094/imoveis/11/41 //本地主机:57094 / imoveis / 11/41

//localhost:57094/imoveis/11/41/258 //本地主机:57094 / imoveis / 11/41/258

But see Is returned the id. 但是请参阅返回ID。 Can I return the name of Estate, City and Region, by keeping my action with ids [int]? 通过使用ID为[int]的操作,我可以返回地产,城市和地区的名称吗?

you can use Attribute Routing 您可以使用属性路由

in route config: 在路由配置中:

routes.MapMvcAttributeRoutes();

Noe you can decorate you action with a route attribute Controller: 不,您可以使用路径属性Controller装饰您的操作

        [Route("~/Prop/{StateInformed?}")]
        public ActionResult Test(string StateInformed = null)
        {
            // DO STAFF

            return View();
        }

        [HttpPost]
        [Route("~/Prop/{StateInformed?}")]
        public ActionResult Test(int? idState)
        {
            // DO STAFF

            String state = string.Empty;

            if (idState.HasValue)
            {
            state = "MyState";
            }

            // if state is null the route will be "/Prop" else "/Prop/MyState"
            return RedirectToAction("Test", new { StateInformed =state});
        }

View: 视图:

@using (Html.BeginForm())
{
   @Html.TextBox("idState")
   <input type="submit" value="BUSCAR" />
}

Examples here 这里的例子

Just play with it 只是玩而已

您可以将非强制性的属性设置为可选,或者如果不想让这些属性可选,则可以为每个属性组合创建单独的方法。

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

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