MVC

13 views
Skip to first unread message

Thom Shannon

unread,
Sep 14, 2009, 6:25:08 AM9/14/09
to liverpoolus...@googlegroups.com
anyone else using it much?

The theory sounds great, but when you actually get into it's quite hard to work how to fit an application into it. Examples and docs seem to be really lacking and most of the code you find is out of date. I'm trying to figure out how to get some search results to update from a couple of different search types at the top of the page, eg.
Search by code |___| <Go> or Search by Department |_______| <Go>

So I have two forms, I thought they'd both submit back to the same Index action and just pass a param of either the code or department name, but there's no way to do that! You can't have two actions with the same name let alone param data type. I can have one form with two params on the action that match up based on the name but then I don't know which button they clicked, or what box they hit enter in.

Any ideas?

--
* Want to become an expert Digital Marketer? www.glow-internet.com/bootcamp

Glow New Media
t: 0151 707 9770
m: 07730 987 574
www.glow-internet.com

Suite 712 Gostins Building
32-36 Hanover Street
Liverpool
L1 4LN

Follow me at http://twitter.com/tshannon

Map: http://tinyurl.com/2f5nxd

Terms & Conditions Apply: http://www.glow-internet.com/home/terms.aspx

Donovan Hide

unread,
Sep 14, 2009, 6:32:54 AM9/14/09
to liverpoolus...@googlegroups.com
Hi Thom,

you've got a few options:

- Build a url which describes the form submit and redirect through
routing to the appropriate action, eg:

/search/code/ goes to SearchController with action Code

/search/department/ goes to SearchController with action Department

- Add a form field which describes the search type and do a switch on
the action parameter, eg:

all POST's go through /search/ to SearchController with action Search

form fields:

Term = ExampleCode0023
Type = Code

and

Term = BiologyDepartment
Type = Department

It's a bit different from the event model of ASP.NET, but once you get
the hang of it you don't really have any limits to what you can do.
And it makes it so much easier to embed a Google Search form on your
page that works!!!

Cheers,
Donny.

2009/9/14 Thom Shannon <th...@glow-internet.com>:

Derek Fowler

unread,
Sep 14, 2009, 6:35:15 AM9/14/09
to liverpoolus...@googlegroups.com
You can have actions with the same name but you need to use the AcceptVerb attribute and specify which verbs each is responsible for i.e. have one of them handle the POST from the search form. Easiest thing is probably to just have the action method expect two strings from the form and check IsNullOrEmpty on each and decide what to do based on that.


2009/9/14 Thom Shannon <th...@glow-internet.com>



--
Derek Fowler
m. +44 (0) 7966 512 369
e. dezf...@gmail.com

Donovan Hide

unread,
Sep 14, 2009, 6:35:35 AM9/14/09
to liverpoolus...@googlegroups.com
Actually, re-reading your post it seems like your problem is how to
re-render the page after a post. Simple answer is don't, maintain
state in the client and do an AJAX post with some jQuery to an action
which returns just the search results and inserts them into the DOM.
The Postback and ViewState models don't really transfer to MVC.

Cheers,
Donny.

2009/9/14 Donovan Hide <donov...@gmail.com>:

Paul Kinlan

unread,
Sep 14, 2009, 6:37:10 AM9/14/09
to liverpoolus...@googlegroups.com
Hi Thom.

Used it quite a bit.

I find it better to separate everything out, sometimes a controller action for each form is best.  In your case I would have a Search action on the the controller that you are using.  Then on your view (web page) have two forms with the same method and action but with different hidden variables indicating the type.

// Controller action on the CompanyController
public ActionResult Search(string type, string term)
{
}


//In your view
<% using (Html.BeginForm("Search","Company")) {%>    
    <%= Html.TextBox("term") %>
    <%= Html.Hidden("type", "department") %>
    <input type="submit" value="Find" />
<% } %>

<% using (Html.BeginForm("Search","Company")) {%>    
    <%= Html.TextBox("term") %>
    <%= Html.Hidden("type", "code") %>
    <input type="submit" value="Find" />
<% } %>

But you get the freedom of who you want to do it and how you choose to structure your controllers.

Paul

2009/9/14 Thom Shannon <th...@glow-internet.com>

Thom Shannon

unread,
Sep 14, 2009, 7:13:46 AM9/14/09
to liverpoolus...@googlegroups.com
Thanks guys! That was all helpful.

We should get the luodn meetings going again :)

Derek Fowler

unread,
Sep 14, 2009, 7:26:01 AM9/14/09
to liverpoolus...@googlegroups.com
Bit far for me to come now - suppose I could get a flight.

2009/9/14 Thom Shannon <th...@glow-internet.com>

Adam Cooper

unread,
Sep 14, 2009, 4:13:43 PM9/14/09
to liverpoolus...@googlegroups.com
You could also have two forms with different Ajax.ActionLinks specified and then return a partial with search results from each one.
To be honest I found the transition to mvc a bit painful, but like others have said well worth it once you can break out of the old webforms mindset and once you add jquery into the mix it's really fun. If you want I've a copy of the ASP.NET MVC 1.0 lying around I'm more than happy to lend. I'd also recommend the ASP.NET Framework book but I'm still half way through it so you'll have to wait a while for that one.
I think getting the luodn meetings going is a great idea btw.

Thom Shannon

unread,
Sep 15, 2009, 6:20:21 AM9/15/09
to liverpoolus...@googlegroups.com
Another question for you all.

I have a partial view and I want it to perform a simple action on the server, but return the user to the current page. Do I have to post to another action and then get it to redirect back to that page? What's the right way to do that redirect while preserving all the parameters etc? Just put the url in a hidden input or something? :-/

I get how I can use ajax but I don't want to depend on that, I like to stick to progressive enhancement and get it all working first without.

Derek Fowler

unread,
Sep 15, 2009, 6:26:08 AM9/15/09
to liverpoolus...@googlegroups.com
I dunno what the "right" answer is but could your controller inherit from a common controller that provides the action method for your partial? You'd have to record what the current action was so you could redirect back to that action when your partial one is done.

2009/9/15 Thom Shannon <th...@glow-internet.com>

Donovan Hide

unread,
Sep 15, 2009, 6:30:00 AM9/15/09
to liverpoolus...@googlegroups.com

Thom Shannon

unread,
Sep 15, 2009, 6:34:56 AM9/15/09
to liverpoolus...@googlegroups.com
I've used TempData a couple of times, it looks like a bit of a hack really. I think I'll go with the hidden input option.

Michael James

unread,
Sep 15, 2009, 6:38:48 AM9/15/09
to liverpoolus...@googlegroups.com
Personally I think the hidden input is more of a hack, tempdata is
provided by the framework to temporarily hold stuff whilst you perform
some logic, this sounds ideal for what you want to do. That way you
know there's no tampering.

Just my thoughts anyway.
--
Regards,

Michael James
m. 07793-113-556
e. mi...@mjjames.co.uk
twitter. mjjames

Donovan Hide

unread,
Sep 15, 2009, 6:39:25 AM9/15/09
to liverpoolus...@googlegroups.com
This chap just uses some lambdas and model mapping with
RedirectToAction to pass his state around actions:

http://jonkruger.com/blog/2009/04/06/aspnet-mvc-pass-parameters-when-redirecting-from-one-action-to-another/

Kind of depends how you've set up your models really.

2009/9/15 Thom Shannon <th...@glow-internet.com>:

Thom Shannon

unread,
Sep 15, 2009, 6:43:41 AM9/15/09
to liverpoolus...@googlegroups.com
but how do I get the current page into tempdata in the first place? I'd have to put it in there during the execution of every single action, plus it's session scoped so if the user has more than one page open they'll get redirected back to the last action executed rather than the one they just came from.

Donovan Hide

unread,
Sep 15, 2009, 6:57:09 AM9/15/09
to liverpoolus...@googlegroups.com
Ahh, session state, another ASP.NET legacy! It doesn't translate well
to MVC. Current page can be determined by your url, or current action.
Might be worth looking at filters if you need something to be run
before every single action. Create a BaseController and define your
filters in there.

Are you doing a search form that will be on every single page of a site?

2009/9/15 Thom Shannon <th...@glow-internet.com>:

Thom Shannon

unread,
Sep 15, 2009, 7:11:06 AM9/15/09
to liverpoolus...@googlegroups.com
It's a "switch user" for admin.

It's working ok now, like so...

using (Html.BeginForm("ChangeClient", "Admin", FormMethod.Post, new { id = "ChangeUser" }))

{ %>

    <%= Html.DropDownList("clientid", (from c in new DataDataContext().Clients orderby c.Name select new SelectListItem() { Value = c.ClientID.ToString(), Text = c.Name, Selected = (Session["CurrentClient"] == null ? false : c.ClientID == Session["CurrentClient"] as int?) }).ToList())%>

    <%= Html.Hidden("currentpage", Page.Request.Url.PathAndQuery)%>

    <input type="submit" value="Change" />

<%

 

 

 

public ActionResult ChangeClient(int clientid, string currentpage)

{

    Session["CurrentClient"] = clientid;

    return Redirect(currentpage);

}

Reply all
Reply to author
Forward
0 new messages