开发者

MVC form values post to route

开发者 https://www.devze.com 2023-01-01 18:35 出处:网络
I\'m not sure if what I am 开发者_如何学Gotry to do is possible but here goes. I have a form with two drop down lists - category and subcategory. When a category is selected an AJAX call is made to g

I'm not sure if what I am 开发者_如何学Gotry to do is possible but here goes.

I have a form with two drop down lists - category and subcategory. When a category is selected an AJAX call is made to get the list of subcategories. For demo purpose I've selected a category of 'Fruit' which shows all of the subcategories for this group.

<select id="category" name="category">
    <option value="Fruit">Fruit</option>
    <option value="Vegetables">Vegetables</option
    <option value="Dairy">Dairy</option>
</select>

<!--AJAX call is made to get sub categories depending on what is selected in category-->
<select id="subcategory" name="subcategory">
    <option value="Apples">Apples</option>      
    <option value="Pears">Pears</option>
    <option value="Bananas">Bananas</option>
</select>

Now if I selected Fruit and then Apples and submitted this form, I'd like it to post to a particular controller /{category}/{subcategory} e.g. /Fruit/Apples and this is dynamic depending on what is selected from the drop downs. So if I selected Fruit and then Pears it would post to /Fruit/Pears

Is this possible and if so how do I do this?

Thanks in advance.


You could modify the form action you are posting to when the user chooses a subcategory:

$(function() {
    $('#subcategory').change(function() {
        $('#someForm').attr(
            'action', 
            '/' + $('#category').val() + '/' + $(this).val()
        );
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消