asp.net-mvc-2

ASP.NET MVC Required Field Indicator

Deadly 提交于 2019-11-28 23:57:14
For fields in my ASP.NET MVC view that have been attributed as required, is there any way for the framework to render some sort of indicator automatically that the field is marked as required in metadata? Should be able to do this with CSS since MVC3 adds in those custom attributes to the element: <input data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" value="" /> You could key off the data-val-required in CSS like so: input[data-val-required] { background:red } or set a background image of an asterisk etc. I did that way because my

ASP.NET MVC Html.ActionLink Maintains Route Values

こ雲淡風輕ζ 提交于 2019-11-28 23:41:39
I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink() keeping route value I don't want However, the final solution is a kludge, pure and simple and I really would like to understand why this happens, if someone can please explain it to me? For completeness, it is possible to recreate the scenario very easily: Create a new MVC web app. Run it up. Visit the About tab Modify the URL to read /Home/About/Flib - This obviously takes you to the action with an id of 'Flib' which we don't care about. Notice that the top menu link to About now actually links to /Home

How to prevent IIS7 for handling HTTP status code 401?

别说谁变了你拦得住时间么 提交于 2019-11-28 23:31:25
I'm working my ASP.NET MVC 2 project. I create exception filter for catching unauthorized access exception that occur when user does not has permission to view some action. [CustomError(typeof(UnauthorizedAccessException), "Error", "UnauthorizedAccess")] public class MyController : BaseController { } After exception has been thrown, my filter will transfer to configured controller/action that is the following method. public ActionResult UnauthorizedAccess(ExceptionContext context) { Response.StatusCode = CustomHttpStatusCode.UnauthorizedUser; return View(model); } Finally, before ASP.NET

MVC 2 vs MVC 3 custom validation attributes using DataAnnotationsModelValidatorProvider.RegisterAdapter

走远了吗. 提交于 2019-11-28 23:28:32
I read on some post, but cant find it now that in MVC 3 it was not really needed to create a Validator, only the Attribute. Is this true? I do say I find it confusing that the attribute has the IClientValidatable on it. So what does the DataAnnotationsModelValidator class do if the annotation has the client side script name (IClientValidatable), and the ability to validate (ValidationAttribute IsValid)? It would be really nice if I didnt have to register the Attribute with the Validator in the global. Can this be done? Did I read some bad advise? EDIT: Interestingly enough I just tested it by

Why mvc Html.HiddenFor does not render my field?

孤人 提交于 2019-11-28 23:21:56
I'm trying to do this simple thing <%= Html.HiddenFor(model => model.Id)%> the model is [HiddenInput(DisplayValue=true)] public int Id { get; set; } but i always get this rendered <input type="hidden" value="0" name="UserInfo.Id" id="UserInfo_Id"> i've check and the id is NOT 0.. ?! need some explanation here... Edit The problem seem's to be the post thing mentionned below. This is working <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" /> Thanks to Manaf Manaf Abu.Rous I'm not sure if this is

ASP.NET MVC and ViewState

走远了吗. 提交于 2019-11-28 22:47:43
问题 Now I've seen some questions like this, but it's not exactly what I want to ask, so for all those screaming duplicate, I apologize :). I've barely touched ASP.NET MVC but from what I understand there is no ViewState/ControlState... fine. So my question is what is the alternative to retaining a control's state? Do we go back to old school ASP where we might simulate what ASP.NET ViewState/ControlState does by creating hidden form inputs with the control's state, or with MVC, do we just assume

Set session variable in Application_BeginRequest

夙愿已清 提交于 2019-11-28 22:26:31
I'm using ASP.NET MVC and I need to set a session variable at Application_BeginRequest . The problem is that at this point the object HttpContext.Current.Session is always null . protected void Application_BeginRequest(Object sender, EventArgs e) { if (HttpContext.Current.Session != null) { //this code is never executed, current session is always null HttpContext.Current.Session.Add("__MySessionVariable", new object()); } } Pankaj Try AcquireRequestState in Global.asax. Session is available in this event which fires for each request: void Application_AcquireRequestState(object sender,

ASP.NET MVC 2 loading partial view using jQuery - no client side validation

寵の児 提交于 2019-11-28 21:39:06
I am using jQuery.load() to render a partial view. This part looks like this: $('#sizeAddHolder').load( '/MyController/MyAction', function () { ... }); The code for actions in my controller is the following: public ActionResult MyAction(byte id) { var model = new MyModel { ObjectProp1 = "Some text" }; return View(model); } [HttpPost] public ActionResult MyAction(byte id, FormCollection form) { // TODO: DB insert logic goes here var result = ...; return Json(result); } I am returning a partial view that looks something like this: <% using (Html.BeginForm("MyAction", "MyController")) {%> <%=

Dynamic list of checkboxes and model binding

白昼怎懂夜的黑 提交于 2019-11-28 21:32:07
I'm trying to create a view that contains a list of checkboxes that is dynamically created from a database, and then retrieve the list of selected ones when the form is posted back. My EF model contains a class: public class ItemIWouldLikeACheckboxFor { public int Id { get; set; } public string Description { get; set; } } I have a view model that contains a list of these: public class PageViewModel { // various other properties public List<ItemIWouldLikeACheckboxFor> checkboxList { get; set; } } My controller get method: public ActionResult Create() { var viewModel = new PageViewModel();

How do I implement custom Principal and Identity in ASP.NET MVC?

做~自己de王妃 提交于 2019-11-28 21:24:07
问题 I want to store extra information in the authenticated user so that I can have it easily accessible (like User.Identity.Id, for example), instead of just the name, since I'm planning on having that non-unique. So far I've gathered that I should look to implement custom Principal and/or Identity, but I'm not sure how to go about it. I've been looking for documentation and tutorials on the matter, but I've found related stuff in different places and I've found it a bit confusing. I have seen