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.
- 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!!!
> 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.
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.
> 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.
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.
> - 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 <t...@glow-internet.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.
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)
{
> 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.
> 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.
> 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.
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.
>> 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.
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.
> 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.
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.
> 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.
> Thom Shannon wrote:
> 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.
> 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.
>> 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.
>> Thom Shannon wrote:
>> 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.
>> 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.
>>> 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.
>>> Thom Shannon wrote:
>>> 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.
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.
On Tue, Sep 15, 2009 at 11:34 AM, Thom Shannon <t...@glow-internet.com> wrote:
> 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.
> 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.
> 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.
> Thom Shannon wrote:
> 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.
> 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.
> 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.
> Thom Shannon wrote:
> 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.
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.
Michael James wrote: > 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.
> On Tue, Sep 15, 2009 at 11:34 AM, Thom Shannon <t...@glow-internet.com> wrote:
>> 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.
>> 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.
>> 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.
>> Thom Shannon wrote:
>> 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.
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?
> 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.
> Michael James wrote:
> 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.
> On Tue, Sep 15, 2009 at 11:34 AM, Thom Shannon <t...@glow-internet.com>
> wrote:
> 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.
> 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.
> 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.
> Thom Shannon wrote:
> 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.
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())%>
}
Donovan Hide wrote:
> 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?
>> 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.
>> Michael James wrote:
>> 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.
>> On Tue, Sep 15, 2009 at 11:34 AM, Thom Shannon <t...@glow-internet.com>
>> wrote:
>> 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.
>> 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.
>> 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.
>> Thom Shannon wrote:
>> 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.