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?
You can still employ input type="hidden" , but initialize it with a value from the ViewData or for Razor, ViewBag . Then you can pass data between the client and server via ajax. For example, using jQuery: $.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With