asp.net-mvc-2

localize default model validation in mvc 2

空扰寡人 提交于 2019-11-28 07:41:09
[Required] [DisplayName("my date")] public DateTime? DateReg { get; set; } so if the user is going to pass in an invalid datetime value he will get this message "The value '02.07.201022' is not valid for my date." how can I translate/localize this message ? Add Messages.resx in App_GlobalResources and in Application_Start in Global.asax : DefaultModelBinder.ResourceClassKey = "Messages"; Then in the Messages.resx file you could define the following string: PropertyValueInvalid: The value {0} is invalid for the property {1} The key needs to be called PropertyValueInvalid . 来源: https:/

How to Use Ajax.BeginForm OnSuccess and OnFailure Methods?

99封情书 提交于 2019-11-28 07:26:17
I using this Ajax.BeginForm <% using( Ajax.BeginForm( "Create","Mandate", new AjaxOptions( ) { OnSuccess = "GoToMandates", OnFailure = "ShowPopUpError" } ) ) {%> <% } %> What do I need to write in the controler to catch this OnSucces and OnFailure. Because OnSuccess I need to show Success message OnFailure I need to show othere message. In my Controller Public ActionResult GetSomething(FromCollection collection) { if(exists == null) { //OnSuccess } else { //OnFailure } } Can anydboy help me out.. how to catch this? Thanks The OnSuccess and OnFailure looks like they are expecting javascript

Getting index value on razor foreach

寵の児 提交于 2019-11-28 06:42:36
I'm iterating a List<T> in a razor foreach loop in my view which renders a partial. In the partial I'm rendering a single record for which I want to have 4 in a row in my view. I have a css class for the two end columns so need to determine in the partial whether the call is the 1st or the 4th record. What is the best way of identifying this in my partial to output the correct code? This is my main page which contains the loop: @foreach (var myItem in Model.Members){ //if i = 1 <div class="grid_20"> <!-- Start Row --> //is there someway to get in for i = 1 to 4 and pass to partial? @Html

Using a dash (-) in ASP.MVC parameters

无人久伴 提交于 2019-11-28 06:35:33
<% using (Html.BeginForm("SubmitUserName")) { %> <input type='text' name='user-name' /> <input type='submit' value='Send' /> <% } %> What should be a signature of a corresponding Action method to accept user-name parameter? public ActionResult SubmitUserName(string user-name) {...} Method signature above does not work for some reason ;-) I know there is an ActionNameAttribute to handle situation with a dash in action name. Is there something like ParameterNameAttribute ? As everyone has noted, the easiest fix would be not to use a dash. If you truly need the dash, you can create your own

Iframe, cross-domain cookies, p3p policy, and safari with error: A required anti-forgery token was not supplied or was invalid

♀尐吖头ヾ 提交于 2019-11-28 06:02:56
I asked this question a while back and found that IE blocks cross-domain cookies in an iframe unless you set a p3p policy . So far, the p3p fix has worked beautifully in ie. However, now we are getting the same error in safari. I found an article with a different p3p policy for safari. I added this code to set up the p3p policy, but I am still getting a request verification token error. public static void SetP3PCompactPolicy() { HttpContext current = HttpContext.Current; if (current.Request.UserAgent.ToLower().IndexOf("safari") >= 0) HttpContext.Current.Response.AddHeader("p3p", "CP=\"IDC DSP

Populating Dropdownlist Using MVC2 Based On Another Dropdownlist (Cascading DropDownList)

自闭症网瘾萝莉.ら 提交于 2019-11-28 05:35:19
I am making an application that deals with vehicles. I need two DropDownLists: Makes: All Vehicle Makes Models: Models that belong to the selected value of the Make DropDownList How is this done in MVC2? My Idea: Do I use an ajax call when my first list is selected and then pull back the Models to bind to the Model DDL? How would model binding come into play that way? UPDATE I posted what I ended up doing as an answer. It is super simple and works great. You can use a get too if you feel so inclined, but you have to specify that you want to like so... return Json(citiesList,

ASP.NET MVC - Alternative for [Bind(Exclude = “Id”)]

▼魔方 西西 提交于 2019-11-28 05:27:14
Is there an alternative for [Bind(Exclude = "Id")] ( Related Question) ? Could I write a model binder? Yes there is: it's called view models. View models are classes which are specifically tailored to the specific needs of a given view. So instead of: public ActionResult Index([Bind(Exclude = "Id")] SomeDomainModel model) use: public ActionResult Index(SomeViewModel viewModel) where the view model contains only the properties which need to be bound. Then you could map between the view model and the model. This mapping could be simplified with AutoMapper . As best practice I would recommend you

MSBuild DeployOnBuild=true not publishing

故事扮演 提交于 2019-11-28 05:15:56
I have a Visual Studio 2010 MVC2 web application that I'm building via the command line using Hudson. I'd like to make Hudson publish a web output, so I added the DeployOnBuild=true and CreatePackageOnPublish=True tags to my command line. My command is: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /target:Clean,Build /property:Configuration=Debug;DeployOnBuild=True;CreatePackageOnPublish=True; [my project name.csproj] Running this command on my development machine (Windows 7) successfully publishes a web output to \obj\Debug\Package\PackageTmp\ . But running it on the Hudson

How can we set authorization for a whole area in ASP.NET MVC?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 04:49:59
I've an Admin area and I want only Admins to enter the area. I considered adding the Authorized attribute to every controller in the Admin area. Isn't there an elegant solution or is this feature not there in the framework itself? EDIT: I'm sorry, I should to have mentioned this before. I'm using a custom AuthorizedAttribute derived from AuthorizeAttribute. Web.config-based security should almost never be used in an MVC application. The reason for this is that multiple URLs can potentially hit a controller, and putting these checks in Web.config invariably misses something. Remember -

How to get the HTML id generated by asp.net MVC EditorFor

谁说胖子不能爱 提交于 2019-11-28 04:37:15
I use the HTML Helpers to render my fields: <%=Html.EditorFor(m => m.User.Surname)%> and the output would be something like this: <input class="text-box single-line" id="User_Surname" name="User.Surname" type="text" value="" /> The helper uses the className + '.' è fieldName to build the id. I need to pass the id to a jQuery function. Is there a way to have it without hardcoding it? Maybe an helper which can use the ASP.NET MVC2 conventions? ASP.NET MVC 4 has Html.IdFor() built in that can return this: @Html.IdFor(m => m.User.Surname) John Landheer See this question: get the generated clientid