views:

1688

answers:

7

Hi all,

I'm an asp.net MVC newbie - very use to webforms!

I have form area in my view. If I click button A I want to submit to /Books/1, but if I click button B I want to submit to /Books/2

How do I go about achieving this with MVC?

Thanks in advance

Nick

+1  A: 

MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.

<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>


<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>
Codewerks
A: 

thanks for the reply - what if I want the same textbox input field in both forms though?

Nick

Nick Swan
A: 

I think you can use Javascript

thanks for the reply - what if I want the same textbox input field in both forms though?

Fromeast
A: 

I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.

stimms
A: 

I guess what I am trying to achieve is paging as you would in a datagrid. I was planning on using the number in the url being posted as being the next page to go forwards or backwards to. The Textbox value I am looking to maintain are the search terms that are used to populate the results table.

Perhaps with the above you might be able to tell me I'm approaching this in the wrong way?

Thanks Nick

Nick Swan
+1  A: 

It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:


    routes.MapRoute(
        "Books",
        "Books/{searchExpr}/{pageId}",
        new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
        );

I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the "Search" button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.

mmacaulay
thanks everyone for their comments. I realized last night and while browsing around StackoverFlow using the Urls differently is the way to go. Going to take a little while to get my head away from thinking the web form way! Expect more questions soon! :)
Nick Swan
+2  A: 
<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>
Corin