1 Question)) - How can I treat a form data and pass it to UpdateModel? For example, I have a input text named "url", and if the user writes
http://www.blablabla.com, I want to replace that to
www.blablabla.com. But, in my UpdateModel, I have:
this.UpdateModel(myClass, new [] { "url" });
I've tried to make a Replace like this:
var myUrl = form["url"].Replace("http://", " ");
this.UpdateModel (myClass , new [] { myUrl } );
But, it didn't work. So, how can I treat a form data and pass it to the UpdateModel? I know how to do it in jQuery, but I really wanna know if there is a way to do that in my ActionResult or something like that.
2 Question )) - In this same form described above, when I submit the form, I redirect to the same action, in case the user wants to register another address (it's an address form). But, when I submit, the page reloads and all the form inputs remain filled
with what the user typed. Is there a way to this refresh to the same Action and clear all form inputs?
I've done that by Redirecting this post action to another Action and this Another Action will lead me back to my get Action. This way, what I want to do works just fine, but I think it was not quite elegant to do this all "redirects to Action". Is there
an elegant and more efficient way?
Question 1: Replace method creates and returns new instance of string (because string is immutable type). So you have to assign returned value:
s = s.Replace("some", "any");
Question 2: Do you really do redirect (RedirectToAction or RedirectToRoute methods)? It is strange... I would try
RouteData.Values.Clear();
Don't forget to click "Mark as Answer" on the post that helped you.
But, in my UpdateModel, I couldn't do this (just remembering that "form" is my FormCollection parameter):
var example = form[ "url" ];
this.UpdateModel (myClassObject, new [] { "name", "birth", example };
It gives no error, but the var example content isn't recorded into DB (it records null or empty). And even if I do: var example = Convert.ToString(form[ "url" ] doesn't work.
All I wanna do is treat a data (with Replace, Split or whatever I need to do) and do the InsertOnSubmit(myClassObject).
Question 1: In my first answer I told how to use Replace method in the right way only.
I think that better way would be writing custom partial methods (i. e. OnNameChanged()) if you are using LINQ To SQL or Entity Framework and do all this stuff in partial methods.
But if you want to supply changed data to UpdateModel method then you should know that
UpdateMethod uses only data from controllers ValueProvider.
a) var old = this.ValueProvider; this.ValueProvider = form; /* call UpdateModel */ this.ValueProvider = old;
So you could save actual value-provider, set (modified) instance of FormCollection as actual value-provider, call
UpdateModel in normal way and then restore original value-provider.
b) By default controllers ValueProvider property value is instance of
DefaultValueProvider which reads data from RouteData, requests
QueryString and requests Form. So you could change content of RouteData,
Request.QueryString od Request.Form. But I think that solution
a) is better.
Question 2: Behavior of your code is very strange (IMHO). Could you provide your code? You could try just calling
RouteData.Values.Clear(); method from your controller but I am not sure if it helps.
Don't forget to click "Mark as Answer" on the post that helped you.
About Question 1, how can I use this ValueProvider to do a Replace() in a FormCollection data and pass it already modified to the UpdateModel, taking for example the code I've sent you ???
I'm using LINQ to SQL, but I'm not familiar with custom partial methods... I'm still gotta know a lot of MVC things!! :-(
it worked just fine what you've told me!! But, I was wondering why do I have to turn back ValueProvider to its original state, at the end of the code, as in:
this.ValueProvider = old;
Couldn't I just set the new ValueProvider and keep it that way?
But, I was wondering why do I have to turn back ValueProvider to its original state, at the end of the code
You don't have to do this. But if some code follows that calls [Try]UpdateModel method too then it would be better to set this property to its original value. If [Try]UpdateModel calls don't follow then you probably don't have to set property
back. I wrote probably because it is possible that some other component can use
Controller.ValueProvider property and then this component will not work properly.
Don't forget to click "Mark as Answer" on the post that helped you.
acymiranda
Member
23 Points
49 Posts
2 Questions: UpdateModel and Form State
Nov 26, 2008 07:20 PM|LINK
Hi everyone!!
I have two questions for you today! hehehe
1 Question)) - How can I treat a form data and pass it to UpdateModel? For example, I have a input text named "url", and if the user writes http://www.blablabla.com, I want to replace that to www.blablabla.com. But, in my UpdateModel, I have:
this.UpdateModel(myClass, new [] { "url" });
I've tried to make a Replace like this:
var myUrl = form["url"].Replace("http://", " ");
this.UpdateModel (myClass , new [] { myUrl } );
But, it didn't work. So, how can I treat a form data and pass it to the UpdateModel? I know how to do it in jQuery, but I really wanna know if there is a way to do that in my ActionResult or something like that.
2 Question )) - In this same form described above, when I submit the form, I redirect to the same action, in case the user wants to register another address (it's an address form). But, when I submit, the page reloads and all the form inputs remain filled with what the user typed. Is there a way to this refresh to the same Action and clear all form inputs?
I've done that by Redirecting this post action to another Action and this Another Action will lead me back to my get Action. This way, what I want to do works just fine, but I think it was not quite elegant to do this all "redirects to Action". Is there an elegant and more efficient way?
thaaaks a lot and sorry for my big post!!!
Augi
Contributor
6730 Points
1142 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 26, 2008 09:03 PM|LINK
Question 1: Replace method creates and returns new instance of string (because string is immutable type). So you have to assign returned value: s = s.Replace("some", "any");
Question 2: Do you really do redirect (RedirectToAction or RedirectToRoute methods)? It is strange... I would try RouteData.Values.Clear();
acymiranda
Member
23 Points
49 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 26, 2008 11:53 PM|LINK
...
My IE wnet crazy and my post went doubled! Sorry!
acymiranda
Member
23 Points
49 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 26, 2008 11:59 PM|LINK
Hi Augi!
Question 1)) In agreement to you've told me, I would have to do this:
form["url"] = form["url"].Replace("some", "any"); ?????
But, in my UpdateModel, I couldn't do this (just remembering that "form" is my FormCollection parameter):
var example = form[ "url" ];
this.UpdateModel (myClassObject, new [] { "name", "birth", example };
It gives no error, but the var example content isn't recorded into DB (it records null or empty). And even if I do: var example = Convert.ToString(form[ "url" ] doesn't work.
All I wanna do is treat a data (with Replace, Split or whatever I need to do) and do the InsertOnSubmit(myClassObject).
My ActionResult is like this:
Question 2)) Augi, I never heard of RouteData.Values.Clear... could you give an example?
Thanks once again!!
Augi
Contributor
6730 Points
1142 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 27, 2008 08:03 AM|LINK
Question 1: In my first answer I told how to use Replace method in the right way only.
I think that better way would be writing custom partial methods (i. e. OnNameChanged()) if you are using LINQ To SQL or Entity Framework and do all this stuff in partial methods.
But if you want to supply changed data to UpdateModel method then you should know that UpdateMethod uses only data from controllers ValueProvider.
a) var old = this.ValueProvider; this.ValueProvider = form; /* call UpdateModel */ this.ValueProvider = old;
So you could save actual value-provider, set (modified) instance of FormCollection as actual value-provider, call UpdateModel in normal way and then restore original value-provider.
b) By default controllers ValueProvider property value is instance of DefaultValueProvider which reads data from RouteData, requests QueryString and requests Form. So you could change content of RouteData, Request.QueryString od Request.Form. But I think that solution a) is better.
Question 2: Behavior of your code is very strange (IMHO). Could you provide your code? You could try just calling RouteData.Values.Clear(); method from your controller but I am not sure if it helps.
acymiranda
Member
23 Points
49 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 27, 2008 08:54 AM|LINK
Hi Augi!
About Question 1, how can I use this ValueProvider to do a Replace() in a FormCollection data and pass it already modified to the UpdateModel, taking for example the code I've sent you ???
I'm using LINQ to SQL, but I'm not familiar with custom partial methods... I'm still gotta know a lot of MVC things!! :-(
Thaaaanks!!!
Augi
Contributor
6730 Points
1142 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 27, 2008 09:08 AM|LINK
public ActionResult Test(FormCollection form) { form["bla"] = form["bla"].Replace("some", "any"); var old = this.ValueProvider; this.ValueProvider = form; //UpdateModel(model); this.ValueProvider = old; // ... }acymiranda
Member
23 Points
49 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 27, 2008 11:23 AM|LINK
Augi,
it worked just fine what you've told me!! But, I was wondering why do I have to turn back ValueProvider to its original state, at the end of the code, as in:
this.ValueProvider = old;
Couldn't I just set the new ValueProvider and keep it that way?
Thanks!!!
Augi
Contributor
6730 Points
1142 Posts
Re: 2 Questions: UpdateModel and Form State
Nov 27, 2008 11:52 AM|LINK