根据输入框获取不同的url

时间:2021-01-02 11:24:51

I'm using the asp.net MVC pattern of having a url like controller/action/id. This works fine if I navigate to the url directly.

我使用的是asp.net MVC模式,有一个类似于控制器/动作/id的url。如果我直接导航到url,这是可以工作的。

I want to have a page where the user can input the id and click a button (or link) which takes them to the correct url.

我希望有一个页面,用户可以在其中输入id并单击一个按钮(或链接),该按钮将他们带到正确的url。

Do I need to somehow make an action link that points to a varying url based on the input using some client-side script?
Or can I render a Form that GETs the correct url?

我是否需要以某种方式将基于客户端脚本的输入指向不同url的操作链接?或者我可以呈现得到正确url的表单吗?

Is what I'm doing unusual; should I be doing something else? (Note that the number of ids is too long to list).

我所做的是不寻常的;我应该做点别的吗?(请注意,id的数量太长,无法列出)。

1 个解决方案

#1


2  

Assuming you have the default route set up, you can do the following client-side script (make sure it's in the same file though):

假设您已经设置了默认路由,您可以执行以下客户端脚本(请确保它在同一个文件中):

var yourLink = '@Url.Action("controller", "action")';
//should output controller/action/

Then assuming your button has the id myButton and the textbox has the id myTextBox, you can do this jQuery:

然后假设你的按钮有id myButton和文本框有id myTextBox,你可以使用jQuery:

$("#myButton").click(function () {
    location.href = yourLink + $("#myTextBox").val();
    //should output controller/action/5 for example, although you might
    //want to make sure they've put a "correct" value in here
});

#1


2  

Assuming you have the default route set up, you can do the following client-side script (make sure it's in the same file though):

假设您已经设置了默认路由,您可以执行以下客户端脚本(请确保它在同一个文件中):

var yourLink = '@Url.Action("controller", "action")';
//should output controller/action/

Then assuming your button has the id myButton and the textbox has the id myTextBox, you can do this jQuery:

然后假设你的按钮有id myButton和文本框有id myTextBox,你可以使用jQuery:

$("#myButton").click(function () {
    location.href = yourLink + $("#myTextBox").val();
    //should output controller/action/5 for example, although you might
    //want to make sure they've put a "correct" value in here
});