In my MVC web application I have the bellow class:
public class LoginVM
{
[Required]
public string Username { get; set; }
[Required]
public string password { get; set; }
public bool RememberMe { get; set; }
}
And in cshtml file I use EditorForModel to generate the form:
.cshtml
#Html.EditorForModel()
Now I want to know that is there any way to add some arbitrary attributes(for example css class attribute) to the html tag that is generating automatically?
Something like this:
...
[Required]
// Some code like this: [addAttribute("class", "myCssClass")]
public string Username { get; set; }
...
Agree with Comments by CodeCaster and Thorarins. It's not a good idea to mix up your Model with View related Attributes. In addition to using EditorTemplates/DisplayTemplates, you could also use the UIHintAttribute. It can help you to switch between different formats or also stylings within the View.
Some example:
http://www.shawnrenner.com/asp-net-mvc-custom-property-display-using-uihint/
Related
I am creating a User Registration form in ASP.NET MVC3 using razor view engine. I am facing problem to create a dropdownlist for country. Country list come from xml file.
My project hierarchy is as follows
BusinessLayer -> User_Account -> Account_Registration.cs
This is a class library in which I want to create a Model for user registration. The code for user model is as follows
public class Account_Registration
{
public string User_Name { get; set; }
public string User_EmailID { get; set; }
public string User_Password { get; set; }
public string User_RePassword { get; set; }
public DateTime User_BirthDate { get; set; }
public enum_Gender User_Gender { get; set; }
public string User_Address { get; set; }
public string User_City { get; set; }
public string User_State { get; set; }
public IEnumerable<SelectListItem> User_Country { get; set; }
public string User_WebSite { get; set; }
public string User_Description { get; set; }
}
Now I want to know that where I should put country XML file and how can I create dropdownlist for XML file.
My Xml file is as follows
<countries>
<country code="AF" iso="4">Afghanistan</country>
<country code="AL" iso="8">Albania</country>
<country code="DZ" iso="12">Algeria</country>
</countries>
As I have to deploy this project on IIS so I want to know where should I put xml file so that I can access it in Account_Registration model which is in class library project and how to create dropdownlist for population countries.
Thanks
You probably shouldn't read xml file each time you render registration page. That would be one little bottleneck you'd have since hard drive operations are costly. I'd recommend reading it into memory (like at the application startup once and somewhere into the global variable, e.g. Countries).
For rendering your list, I'd recommend looking at the following article. Basically, it goes like this:
Html.DropDownList(“countries”, new SelectList(model.Countries), “CountryId”, “CountryName”))
you can create own extension for DropDown.
public static class GridExtensions
{
public static MvcHtmlString XmlDropDown(this HtmlHelper helper, string name, string value)
{
var document = XDocument.Parce(value);
var model = new List<SelectListItem>();
foreach(XElement element in document.Elements("countries/country"))
{
model.Add(new SelectListItem(){Text=element.Value, Value=element.Attribute("iso").Value})
}
return Html.DropDownList(name, model))
}
}
So, on view you can use
Html.XmlDropDown(“countries”, model.Countries)
I have a model like the followings:
public class MyModel {
[Required]
public string Name { get; set; }
public string Family { get; set; }
[Required]
public int Number { get; set; }
}
So for example in Edit View I have 3 Editorfor() objects and I am interesting to filter the post data of this page, actually I want to ignore Number field and just want to post Name and Family Also I need the validations of Number be active, One way is I remove Number property from MyModel and define in view by hand and write all validation script by own, but I am interesting to know is there any simpler way in MVC. Does anyone have any idea?
Controlling all that validation and model binding manually is way too complicated and error-prone. You should be using ViewModels
public class SomeSpecificViewModel
{
[Required]
public string Name { get; set; }
public string Family { get; set; }
}
public ActionResult SomeSpecificAction(SomeSpecificViewModel model)
{
//...
}
Now MVC wil validate only Name and Family
Any value not filled in the view will not be posted to the controller. However, if a field which is [Required] is not filled, then ViewModel.isValid will be false.
In an asp.net MVC 1.0 application I have the following action
public ActionResult Submit(ContactModel model)
{
ContentResult ret = new ContentResult();
//do something
return ret;
}
Where ContactModel is defined as:
public class ContactModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
public bool Authorization { get; set; }
}
Given that, in the Html Form that posts to this action, the names of the input tags are different from the names of the properties in the class ContactModel. Is there a way to map the html names to the class name so that the model binder can do its job?
The Bind Prefix (see How to use Bind Prefix?),
can partially solve this problem although doesn't allow the definition of full aliases for each property.
The Bind prefix has a strange behaviour though, because it assumes that between the prefix and the name there is a '.' (dot).
I don't think so.
You could create your own modelbinder though. See here for an example
I want to input html in the database and also display it back as html. I wrote my view model like this:
public class TemplateVM
{
[HiddenInput(DisplayValue = false)]
public int TemplateId { get; set; }
public string Name { get; set; }
public string Content { get; set; }
}
the property Content should be able to accept html. How can I do this? Right now, it throws the error of:
A potentially dangerous Request.Form value was detected from the client (Content="<p>test</p>").
I'm aware of using this on the action, but I dont want it to apply to every property.:
[ValidateInput(false)]
Instead of using ValidateInput attribute on entire model, I suggest you use AllowHtml attribute on Content property:
public class TemplateVM
{
[HiddenInput(DisplayValue = false)]
public int TemplateId { get; set; }
public string Name { get; set; }
[AllowHtml]
public string Content { get; set; }
}
This attribute is only applied for Content property, while other properties are still validated.
Put [ValidateInput(false)] on top of TemplateVM. It will apply to all properties.
I know this works for single properties as per Scott Guthrie's blog to auto-magically use a partial view to render a partial model passed to it (UI Helper like in dynamic data):
[UIHint("StateDropDown")]
public string State { get; set;}
But how do you annotate an entire class to use an UI helper like this:
[UIHint("Address")]
public class Address {
public string addr1 { get; set; }
public string addr2 { get; set; }
public string city { get; set; }
[UIHint("StateDropDown")]
public string state { get; set; }
public string zip { get; set; }
}
(Except [UIHint("Address")] doesn't seem to work on classes. I see in his examples, he has "Customer.aspx" in the Shared->EditorTemplates folder, so I assume this is possible.
Make a template with the name of the class, and it just works.