简体   繁体   English

如何获得ASP.NET MVC单选按钮以响应不区分大小写的路由,或来自控制器的指示?

[英]How can I get ASP.NET MVC radio buttons to respond to a case insensitive route, or from directions from the controller?

I have an ASP.NET MVC app wherein I want a certain radio button to be checked based on information supplied in the URL. 我有一个ASP.NET MVC应用程序,其中我希望根据URL中提供的信息检查某个单选按钮。 But (and this is key...), I want the right radio button to be checked regardless of the case of the supplied route parameter. 但是(这是关键...),无论提供的route参数如何,我都希望检查右单选按钮。

I thought I could easily do this with a bit of code in the controller that would take the route parameter of any case and transform it to always be upper case (to match the values assigned to the radio buttons), but so far I have been unsuccessful (and have discovered some things that make me suspect that I do not know exactly how model information is supplied to my view). 我以为我可以用控制器中的一些代码轻松地做到这一点,该代码可以采用任何情况下的route参数并将其转换为大写形式(以匹配分配给单选按钮的值),但是到目前为止,不成功(并且发现了一些使我怀疑我不完全知道如何向视图提供模型信息的东西)。

My route is set up like so in the RouteConfig: 我的路线是在RouteConfig中这样设置的:

routes.MapRoute(
                name: "LegSearch",
                url: "{controller}/{action}/{requestType}/{billNumber}/{sessionNumber}",
                defaults: new { controller = "FNSPublicSearch", action = "Search", requestType = UrlParameter.Optional, billNumber = UrlParameter.Optional, sessionNumber = UrlParameter.Optional }
            );

The Search action in my controller looks like this: 我的控制器中的“搜索”操作如下所示:

[HttpGet]
        public ActionResult Search(string requestType, string billNumber, string sessionNumber)
        {

            var searchModel = new SearchModel();

            string requestTypeUpperCase = null;

            if (!string.IsNullOrWhiteSpace(requestType))
            {
                requestTypeUpperCase = char.ToUpper(requestType[0]) + requestType.Substring(1);
                searchModel.RequestType = requestTypeUpperCase;
            }  

//other search-related code
}

My model: 我的模特:

public class SearchModel : ISearchModel
    {
        [DisplayName("Session year")]
        public string SessionYear { get; set; }

        [DisplayName("Bill or initiative number")]
        public string BillNumber { get; set; }

        [DisplayName("Bill or initiative title")]

        public string BillTitle { get; set; }

        [DisplayName("Bill or initiative")]
        public string RequestType { get; set; }

        public List<ISearchResult> Results { get; set; }

        public List<string> BillNumbers { get; set; } 


        public List<SelectListItem> SessionYears { get; set; }

    }

And finally, here's how the radio buttons are set up on my view: 最后,这是我视图中单选按钮的设置方式:

@model FNSPublicSearch.Models.SearchModel

@using (Html.BeginForm("Search", "FNSPublicSearch", FormMethod.Post, new { id = "SearchForm" }))
{

//some other markup...


@Html.LabelFor(model => model.RequestType, new { @class = "control-label", style="margin-left: 5px"})

@Html.RadioButtonFor(model => model.RequestType, "Bill") Bill                        
@Html.RadioButtonFor(model => model.RequestType, "Initiative") Initiative
@Html.RadioButtonFor(model => model.RequestType, "Both") Both

//some more markup...
}

I've also discovered a couple of things with a little bit of JavaScript at the bottom of my view, and that's what makes me think I don't fully know how these radio buttons are getting selected... 我还在视图的底部发现了一些带有少量JavaScript的东西,这就是让我认为我不完全知道如何选择这些单选按钮的原因...

For instance, 例如,

var selectedType = "@Model.RequestType";

comes out as "Bill," for instance, regardless of whether "Bill" or "bill" is supplied to the route. 例如,无论“ Bill”还是“ bill”提供给该路线,它都会以“ Bill”的形式出现。 I assume that's due to the code I've written in the controller, which is supposed to pass an upper-cased version of the requestType parameter over to the view. 我认为这是由于我在控制器中编写的代码所致,该代码应该将requestType参数的大写版本传递给视图。

Where I get confused, though, is when I do 不过,我感到困惑的是何时

console.log($('input:radio[name="RequestType"]:checked').val());

the output is "Bill" when "Bill" is supplied to the route (great!), but "undefined" when I supply "bill" (huh? what happened to my controller code?). 当将“ Bill”提供给路由时,输出为“ Bill”(很棒!),但是当我提供“ bill”时输出为“ undefined”(呵呵,我的控制器代码怎么了?)。

So, we have expected result: hitting "{controller}/{action}/Bill/{billNumber}/{sessionNumber}" results in the same radio button being checked as ""{controller}/{action}/bill/{billNumber}/{sessionNumber}". 因此,我们获得了预期的结果:按下“ {controller} / {action} / Bill / {billNumber} / {sessionNumber}”会导致同一单选按钮被选中为“” {controller} / {action} / bill / {billNumber } / {sessionNumber}”。

Actual result: uppercase "Bill" in the URL, which matches the value of the Bill radio button, works; 实际结果:URL中与“ Bill”单选按钮的值匹配的大写“ Bill”起作用; that radio button will be checked. 该单选按钮将被选中。 Lowercase "bill" results in none of the radio buttons being checked, I guess because "bill" does not equal the button's value of "Bill" (even though I thought I accounted for that in the controller). 小写的“ bill”不会检查任何单选按钮,我想是因为“ bill”不等于按钮的“ Bill”值(即使我以为我在控制器中考虑了该值)。

I'm fairly new to MVC, so any help is appreciated, thanks! 我对MVC还是很陌生,因此感谢您的任何帮助,谢谢!

Well, I figured out a solution, but I feel like it's pretty hacky. 好吧,我想出了一个解决方案,但是我觉得它很hacky。 I would much prefer someone who knows explain how those radio buttons really receive model information in the original scenario I posted, but anyway... 我更希望有人知道在我发布的原始方案中那些单选按钮如何真正接收模型信息,但是无论如何...

I added a new model property: 我添加了一个新的模型属性:

public string TypeRadio { get; set; }

Then I altered the controller to set that differently-named property based on the original route parameter: 然后,我更改了控制器,以根据原始的route参数设置名称不同的属性:

[HttpGet]
        public ActionResult Search(string requestType, string billNumber, string sessionNumber)
        {
            var searchModel = new SearchModel();

            string requestTypeLowerCase = string.Empty;

            if (!string.IsNullOrWhiteSpace(requestType))
            {
                requestTypeLowerCase = requestType.ToLower();
                searchModel.RequestType = requestTypeLowerCase;
                searchModel.TypeRadio = requestTypeLowerCase;
            } 
//etc...
}

Then I bound my radio buttons to that new, differently-named property: 然后,将单选按钮绑定到这个新的名称不同的属性:

@Html.RadioButtonFor(model => model.TypeRadio, "bill") Bill                        
@Html.RadioButtonFor(model => model.TypeRadio, "initiative") Initiative
@Html.RadioButtonFor(model => model.TypeRadio, "both") Both

Left the route the same, and everything worked, since (I guess?) now the radio button names have nothing to do with the route parameters. 保持路由不变,一切正常,因为(我猜吗?)现在单选按钮名称与路由参数无关。

Thanks for looking, but definitely feel free to enlighten me if you know why my original code didn't work! 感谢您的关注,但是如果您知道我的原始代码为何无法正常工作,请随时给我启发!

暂无
暂无

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

相关问题 如何在ASP.NET MVC中使用带有引导程序的HTML标记从控制器获取结果? - How I can use html tags with bootstrap in asp.net mvc for get a result from controller? 如何使用asp.net mvc和razor模板引擎从c#中获取路由名称的URL? - How can I get the URL from route's name in c# using asp.net mvc and the razor template engine? 从ASP.Net MVC的控制器中的请求中获取#路由 - Getting the # route in from the Request in a controller in ASP.Net MVC ASP.NET MVC 3 - 如何填充数据库中的单选按钮列表 - ASP.NET MVC 3 - How to populate list of radio buttons from database 如何将数据从Select2选项/值(标签)获取到ASP.NET MVC中的控制器? - How do I get the data from a Select2 options/value (tags) to the controller in asp.net mvc? 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project 如何将请求路由到页面控制器以获取ASP.NET MVC 4中不存在的路由 - How can I route requests to a page controller for routes that don't exists in ASP.NET MVC 4 如何将特定的子域请求重新路由到其他ASP.NET MVC控制器? - How can I re-route a specific subdomain request to a different ASP.NET MVC controller? 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC? 如何在ASP.Net MVC实体框架的帐户控制器中传递来自登录方法的ID? - How can I pass the Id from Login method in Account Controller in ASP.Net MVC Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM