问题
I'm passing in some model information to an ActionLink, but I'd also like to provide the action with the values of some inputs on the page. For example, if I had something like this:
<input Name="MyInput" />
<%: Html.ActionLink("MyAction", "MyController", Model.Value);
I'd like the action to be aware of both Model.Value (which is passed in via parameter), and the value of MyInput. Normally I would use a FormCollection, but I can't in this instance since I'm not doing a submit.
So how can I pass the value of MyInput to MyAction? Would I have to add a property named MyInput to my model? Assuming that would work, is there an easier way, or at least one that doesn't involve modifying the model?
回答1:
There is no way to do this in HTML, thus there isn't a way to do this in ASP.NET MVC.
There are two possible solutions to this that you can choose from:
Use JavaScript such that when the user edits the textbox you dynamically change the value of the anchor tag to include what they typed in. You can't use ASP.NET routing for this because that runs on the server and you need client side code.
Do a form submit instead of a link. This is the recommended way in HTML. When the user is submitting data, it should be in a form. Wrap everything in a form tag and place the textbox and a button in there. Set the form's action to be the URL that you want it to post to.
This is the same response as I put in this question, though the asker of the other question might have had a slightly different idea.
回答2:
I determined that I probably could have read the values of the text box via Javascript (I probably would have used JQuery), and passed them as parameters as part of an anonymous type (instead of just using Model.Value). However, I decided to switch from using an action link to just doing a form submit and using the FormCollection. It just made more sense with the page I was building.
来源:https://stackoverflow.com/questions/2030124/mvc2-how-can-i-read-control-values-for-an-action-link