MVC3 Html.BeginForm - passing arguments as RouteValueDictionary fails - asp.net-mvc-3

I have a multi-step setup process where I would like to pass query string arguments appended to the URL only if they are relevant.
http://localhost:6618/Account/Profile?wizard=true&cats=dogs
#using( Html.BeginForm() )
worked great. It yielded: <form action="/Account/Profile?setup=true&cats=dogs" method="post"> i.e. it passed into the POST action any of the original query string parameters, and then in that Controller action I could chose which ones were relevant to pass to my next step, or necessary to add, by adding to the RouteValues and a RedirectToResult.
However, I need to assign a class to my form for styling purposes.
I tried:
#using( Html.BeginForm( "Profile", "Account", args, FormMethod.Post, new { #class = "mainForm" } ) )
which yields:
<form action="/Account/Profile?Count=1&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D" class="mainForm" method="post">
(args was generated by a filter, and is a RouteValueDictionary). The specification http://msdn.microsoft.com/en-us/library/dd505151.aspx indicates that you can pass arguments with a System.Web.Routing.RouteValueDictionary.
What I want is <form action="/Account/Profile?setup=true&cats=dogs" class="mainForm" method="post">
I should mention I would prefer not to do something like passing in new {key = value} instead, since there is a fair amount of logic to determine what I will be passing along to the next step.
Any suggestions on what to do here?
I am stymied by this seemingly simple task, and am surely missing something terribly obvious.

args was generated by a filter, and is a RouteValueDictionary
That's the key point here. In this case make sure you are using the correct overload of the BeginForm method:
#using(Html.BeginForm(
"Profile",
"Account",
args,
FormMethod.Post,
new RouteValueDictionary(new { #class = "mainForm" })
))
{
...
}
Notice the last argument? It must be an IDictionary<string, object> for this to work.
In your example it is this overload that gets picked up. But since you are passing a RouteValueDictionary for the routeValues parameter instead of an anonymous object it gets messed up.
So, you should either have both routeValues and htmlAttributes as dictionaries or both as anonymous objects.

Following will work.
#using (Html.BeginForm("Profile", "Account", new { id=122}, FormMethod.Post, new { #class = "mainForm" }))
the route value is created by object initialize syntax i.e new {key = value}

So you're using this overload:
http://msdn.microsoft.com/en-us/library/dd460542.aspx
Is it possible to make args a simple object with keys and values? I think that might solve your problem.
According to docs:
The parameters are retrieved through reflection by examining the properties of the object. This object is typically created by using object initializer syntax. - this seems to be what is happening-- it's using reflections to get the properties of route dictionary- the properties being keys (collection of string) and values (collection of objects)
Another option would be to not use the html helper and create the form tag manually-- although that would kind of defeat the purpose of having the html helpers.

Just a thought, but, even for a multi-step form, wouldn't you want to either choose to make it all GET or POST? In the example above... it looks like you are using POST with the form... but still trying to use GET along the way.
Why not just use hidden POST values (using HTML INPUTs,) along the way?
Otherwise, users could more-easily change the values, right? (Though, that might not matter in this application. And this is mostly just food for thought.)

Related

MVC 3 - Html.EditorFor seems to cache old values after $.ajax call

This is a follow on from the following question:
MVC 3 + $.ajax - response seems to be caching output from partial view
There is a detailed description of the problem over there. However, I have now managed to narrow down the problem, that seems to be with the Html.EditorFor helpers, hence the new question.
The issue:
I post data to the server using $.ajax, then return the html of the partial view that holds the input controls. The problem is that, despite passing a newly created object to the Partial Views model, the various #Html.EditorFor and #Html.DropDownListFor helpers return the OLD DATA!.
I can prove that the model has correctly passed in a new object to the helpers, by printing the value out beside the Html helper. Ie:
#Html.EditorFor(model => model.Transaction.TransactionDate)
#Model.Transaction.TransactionDate.ToString()
As the following image shows, the #Html.EditorFor is returning the wrong data:
[Note that the value beside the Comentario text box is a date time, because I was testing replacing the default values with a value that would change with each post, ie, a DateTime.]
If I replace the #Html.EditorFor for TransactionDate with a plain old #Html.TextBox():
#Html.TextBox("Transaction_TransactionDate", Model.Transaction.TransactionDate)
Then it renders the correct TransactionDate value for a new Transaction object, ie, DateTime.MinValue (01/01/0001...).
Therefore...
The problem is with the #Html.EditorFor helpers. The problem also happens with TextBoxFor and DropDownListFor.
The problem being that these helpers seem to cache the old value.
What am I doing wrong??!
EDIT:
I have just tried debugging in the custom Editor template for dates, and in there, ViewData.TemplateInfo.FormattedModelValue shows the correct value, ie, "01/01/0001". However, once it gets to Fiddler, the response is showing the old date, eg, "01/09/2011" in the image above.
As a result, I just think that there is some caching going on here, but I have none set up, so nothing makes any sense.
There is no caching involved here. It's just how HTML helper work. They first look at the ModelState when binding their values and then in the model. So if you intend to modify any of the POSTed values inside your controller action make sure you remove them from the model state first:
[HttpPost]
public virtual ActionResult AjaxCreate(Transaction transaction)
{
if (ModelState.IsValid)
{
service.InsertOrUpdate(transaction);
service.Save();
}
service.ChosenCostCentreId = transaction.IdCostCentre;
TransactionViewModel viewModel = new TransactionViewModel();
ModelState.Remove("Transaction");
viewModel.Transaction = new Transaction();
ModelState.Remove("CostCentre");
viewModel.CostCentre = service.ChosenCostCentre;
...
return PartialView("_Create", viewModel);
}
Even if you do not specify caching, it sometimes can occur. For my controllers which handle AJAX and JSON requests, I decorate them as follows:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
This specifically declares no caching should occur.
UPDATE
Based on an answer Darin Dimitrov gave here, try adding the following line to your controller action:
ModelState.Clear();
i have never seen this but basically if you are using ajax to request this data, you need to set nochache: i am assuming you using jQuery.ajax here so will show the code:
$.ajax({
url: "somecontroller/someAction,
cache: false, // this is key to make sure JQUERY does not cache your request
success: function( data ) {
alert( data );
}
});
just a stab in the dark, i assume you have probably already covered this already. have you tried to create a new model first and then populate that new instance of the model with your data, and then send this to your view!
Finally not sure what DB server your using but have you check to see that DB results are not cached and that you are not just requesting SQL results from the DB cache... i dont use MsSQL but i hear that it has outputCaching until something is change on the DB server itself?? anyway just a few thoughts
This was unexpected behavior for me, and although I understand the reason why it's necessary to give ModelState precedence, I needed a way to remove that entry so that the value from Model is used instead.
Here are a couple methods I came up with to assist with this. The RemoveStateFor method will take a ModelStateDictionary, a Model, and an expression for the desired property, and remove it.
HiddenForModel can be used in your View to create a hidden input field using only the value from the Model, by first removing its ModelState entry. (This could easily be expanded for the other helper extension methods).
/// <summary>
/// Returns a hidden input field for the specified property. The corresponding value will first be removed from
/// the ModelState to ensure that the current Model value is shown.
/// </summary>
public static MvcHtmlString HiddenForModel<TModel, TProperty>(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression)
{
RemoveStateFor(helper.ViewData.ModelState, helper.ViewData.Model, expression);
return helper.HiddenFor(expression);
}
/// <summary>
/// Removes the ModelState entry corresponding to the specified property on the model. Call this when changing
/// Model values on the server after a postback, to prevent ModelState entries from taking precedence.
/// </summary>
public static void RemoveStateFor<TModel, TProperty>(this ModelStateDictionary modelState, TModel model,
Expression<Func<TModel, TProperty>> expression)
{
var key = ExpressionHelper.GetExpressionText(expression);
modelState.Remove(key);
}
Call from a controller like this:
ModelState.RemoveStateFor(model, m => m.MySubProperty.MySubValue);
or from a view like this:
#Html.HiddenForModel(m => m.MySubProperty.MySubValue)
It uses System.Web.Mvc.ExpressionHelper to get the name of the ModelState property. This is especially useful when you have "Nested" models since the key name isn't obvious.
Make sure you're not doing this:
#Html.EditorFor(model => model.Transaction.TransactionDate.Date)
I did this, and the model never got the value back. It worked perfectly once I remove the .Date.

prepopulate Html.TextBoxFor in asp.net mvc 3

I'n new at this, so apologies if this isn't explanatory enough. I want to prepopulate a field in a form in asp.net mvc 3. This works;
#Html.TextBox("CompName", null, new { #value = ViewBag.CompName })
But when I want to prepopulate it with a value and send that value to my model, like this;
#Html.TextBoxFor(model => model.Comps.CompName, null, new { #value = ViewBag.CompName })
It won't work. Any ideas?
Thanks in advance!
So, I would suggest is to move to using viewmodels rather than the ViewBag. I made a folder in my project called ViewModels and then under there make some subfolders as appropriate where I put my various viewmodels.
If you create a viewmodel class like so:
public class MyViewModel
{
public string CompName { get; set; }
}
then in your controller action you can create one of those and populate it, maybe from some existing model pulled from a database. By setting that CompName property in the viewmodel, it'll have that value in your view. And then your view can look something like this:
#model MyNamespace.ViewModels.MyViewModel
#Html.EditorFor(model => model.CompName)
or #Html.TextBoxFor would work too.
Then back in your controller action on the post, you've got something like this:
[HttpPost]
public ActionResult MyAction(MyViewModel viewModel)
{
...
// do whatever you want with viewModel.CompName here, like persist it back
// to the DB
...
}
Might be that you use something like automapper to map your models and viewmodels but you could certainly do that manually as well, though the whole lefthand/righthand thing gets quite tedious.
Makes things much easier if you do it this way and isn't much work at all.
Update
But, if you really want to pass that value in view the ViewBag, you could do this:
In your controller action:
ViewBag.CompName = "Some Name";
Then in your view:
#Html.TextBoxFor(model =>model.Comps.CompName, new {#Value = ViewBag.CompName})
And that'll pre-populate the textbox with "Some Name".
I'd still go with the viewmodel approach, but this seems to work well enough. Hope that helps!
From your controller, if you pass a model initialized with default values using one of the View(...) method overloads that accepts a model object, these values will be used by the view when rendered. You won't need to use the #value = ... syntax.

Append to route collection

I have the following route data
object newsRoute = new
{
Area = "Admin",
Controller = "News",
Action = "Edit"
}
How can I append Title = "Hello" to the object routNews?
As in or similar
newsRoute.Append(Title = "Hello");
When you create the anonymous object you've effectively defined the properties of the anonymous class. I don't think you'll have much luck trying to redefine the type after the fact. You could create a new anonymous object with the new field and the original fields and copy the fields over, but I'm guessing you wouldn't want to do this.
You say that the object represents routedata, in that case it is probably a good idea to convert the anonymous object into a RouteValueDictionary instance using the following method
http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.anonymousobjecttohtmlattributes(v=VS.98).aspx
Once converted the object has normal dictionary semantics so you can add new key value pairs at will.
You should then be able to use the RouteValueDictionary to generate your urls

How to set a hidden value in Razor

I know that what I'm trying to do is bad idea, but I have specific constrains for now.
I have multiple sites, using one and the same MVC3 code base. For one of them the requirement is to hide some required fields from the form.
I know that the best approach is to modify the controller to set the defaults for these fields, but I'd like to achieve this modifying only the view for this particular site w/o changing the code.
So, how do I set a particular model property to a default value in the view? The ideal should be something like:
#Html.HiddenFor(model => model.RequiredProperty)
#model.RequiredProperty = "default"
EDIT: more explanation
So, actually this is in a sub-view, which is used by 2 different main views. I need these properties set only if one particular main view is used, and not the others.
So, I guess the setting to default need to go to that particular "main" view. Looks like I can not use HiddenFor in the sub-view, and then Html.Hidden in the main.
Is there a way to check in the sub-view which is the outer view?
If I understand correct you will have something like this:
<input value="default" id="sth" name="sth" type="hidden">
And to get it you have to write:
#Html.HiddenFor(m => m.sth, new { Value = "default" })
for Strongly-typed view.
There is a Hidden helper alongside HiddenFor which lets you set the value.
#Html.Hidden("RequiredProperty", "default")
EDIT Based on the edit you've made to the question, you could do this, but I believe you're moving into territory where it will be cheaper and more effective, in the long run, to fight for making the code change. As has been said, even by yourself, the controller or view model should be setting the default.
This code:
<ul>
#{
var stacks = new System.Diagnostics.StackTrace().GetFrames();
foreach (var frame in stacks)
{
<li>#frame.GetMethod().Name - #frame.GetMethod().DeclaringType</li>
}
}
</ul>
Will give output like this:
Execute - ASP._Page_Views_ViewDirectoryX__SubView_cshtml
ExecutePageHierarchy - System.Web.WebPages.WebPageBase
ExecutePageHierarchy - System.Web.Mvc.WebViewPage
ExecutePageHierarchy - System.Web.WebPages.WebPageBase
RenderView - System.Web.Mvc.RazorView
Render - System.Web.Mvc.BuildManagerCompiledView
RenderPartialInternal - System.Web.Mvc.HtmlHelper
RenderPartial - System.Web.Mvc.Html.RenderPartialExtensions
Execute - ASP._Page_Views_ViewDirectoryY__MainView_cshtml
So assuming the MVC framework will always go through the same stack, you can grab var frame = stacks[8]; and use the declaring type to determine who your parent view is, and then use that determination to set (or not) the default value. You could also walk the stack instead of directly grabbing [8] which would be safer but even less efficient.
While I would have gone with Piotr's answer (because it's all in one line), I was surprised that your sample is closer to your solution than you think. From what you have, you simply assign the model value before you use the Html helper method.
#{Model.RequiredProperty = "default";}
#Html.HiddenFor(model => model.RequiredProperty)
How about like this
public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes)
{
return HiddenFor(htmlHelper, expression, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, IDictionary<string, object> htmlAttributes)
{
return htmlHelper.Hidden(ExpressionHelper.GetExpressionText(expression), value, htmlAttributes);
}
Use it like this
#Html.HiddenFor(customerId => reviewModel.CustomerId, Site.LoggedInCustomerId, null)

MVC pass query strings

I am new to Microsoft.MVC, so sorry if this is a silly question.
I have created a very simple forum, and I am now trying to create some tag functionality to it.
I have a controller where the index retrieves the last 10 forum threads. I would like to pass a query string, or something a-like with the Id to the supplied tag to the forum, so I thereby can get the forum threads, which e.g. have the tag 'ASP.NET'.
If it was a regular webforms project I would simply supply a query string with the tag id, to the index page, and then retrieve the forum threads with the tag, but isn't there a smarter way to do it in MVC.NET?
The reason why I ask, is it seems like a step backwards from REST-urls, to suddenly use 'regular' query strings?
First you define your action (like you probably already did), and add the parameters like you need them:
public ActionResult Forum(string tag, int page)
{
// do your thing
// ...
return View();
}
Then, in your Global.asax.cs, you can add a route that handles the parameters like you want them.
routes.MapRoute("Forum", "Forum/{tag}/{page}", new {controller = "Home", action = Forum"});
This will cause the Forum action to trigger on the HomeController when you go to the http://yourhost/Forum link. If you then click have a link like this http://yourhost/Forum/asp.net/1 Then "asp.net" will passed into the tag parameter, and 1 will be passed onto the page parameter.
You can use an ActionLink html helper. Assuming you have a forums controller and index page, to get a link to /forums/index/1?tag=asp.net you can do:
Html.ActionLink("ASP.NET", "index", new { id = 1, tag = "asp.net"})

Resources