开发者

Passing dynamic data to controller with ASP.NET MVC and jQuery on form submit

I have a website which is basically a single page containing a bunch of dynamic content. In normal operation the user should never leave this page. To handle/report certain errors though, I need to redirect to an error page. So on the error page I want to provide a link back to the normal page which provides the app the information required to rebuild all the dynamic content which was previously open.

I think I can rebuild the page by parsing the querystring with javascript and reloading dynamic content. I'm not sure if this is the best way, but I've got it working. So href in the link on my error page needs to look something like:

MySite/MyController/MyAction?1,5,8,9

where the numbers in the querystring basically indicate the Id's of content sections to load in on document.ready.

I'm now stuck on how to generate this link though. I think I need to pass these numbers into the controller somehow, so the controller can pass them into the error view which will then generate the "back" link.

I have a standard html form for uploading a file:

using (Html.BeginForm("MyAction", "MyController", 
                       FormMethod.Post, 
                       new { enctype = "multipart/form-data" }))

                       { /* ... */ }

I then have a button which calls the j开发者_运维技巧Query .submit() method on the form.

So, the question, when I click on the button which submits the form, how can I attach the additional data so I can access it in my controller?

Hope that's clear, if not let me know and I can provide more detail. Thanks.

Edit: It occured to me that I could submit this information with a hidden field in the form. I'll try that if I can't find a better way, but I'd like a more generic solution if possible as I have a number of forms on the page and ideally I'd like this data available to any/all of them.


your url should be biult just before sending the form with .submit() not in Html.BeginForm tag.... i can't see the problem over here... what exactly you are dealing with?


I know this is not quite what you asked but instead of going through the hassle of redirecting the user in the first place why don't you show a modal window (a jquery based one) that contains an iFrame. The iFrame href can be determined by the calling page and can show the error page.

Not sure if this helps but I have used this technique and found that I do not need to worry about getting the user back to the place where they started from. Additional to this you will not need to rebuilding/reloading the orignal content as it will still be on the calling page.

****EDIT**** After reading your comments maybe you could do the following...

  1. On the link click - open up a modal popup pointing to an iframe.
  2. The iframe calls the action on the controller to download the file.
  3. The view could say "Downloading file... please wait, click here to close etc."
  4. If the action throws the error then redirect to the error page as normal as it is within the iframe modal popup.

This way you do not need to leave the current calling page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜