How to convert a float number to integer in the form in mvc3?
#Html.EditorFor(model => model.CODE)
#Html.ValidationMessageFor(model => model.CODE)
You could decorate the CODE property on your view model with the [DisplayFormat] attribute which allows you to define the format of the float number when it is displayed in the view:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0}")]
public float CODE { get; set; }
Related
I need your help. I am working with MVC3-Razor application. I need to validate a textbox on View (.cshtml file) in such a way that, the starting 2 characters must be "PR" and 4th character must be "2". This is the requirement. How would i achieve this functionality? Any suggestions, it would be great help. Thanks for your precious time.
Model
public class RegisterModel
{
public int ID { get; set; }
[RegularExpression(#"^PR[a-zA-Z0-9]2([a-zA-Z0-9]*)$", ErrorMessage = "Please enter valid Name.")]
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
}
View
#using (Html.BeginForm("DYmanicControllerPage", "Test", FormMethod.Post, new { id = "FrmIndex" }))
{
<div>
#Html.LabelFor(m => m.Name)
#Html.TextBoxFor(m => m.Name)
#Html.ValidationMessageFor(m => m.Name)
</div>
}
When I try to convert date to string I get this error. So how can I fix this error ?
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Here is my view:
<div class="editor-field">
#Html.TextBoxFor(x => x.date.ToString("MM-dd-yyyy"), new { #class = "date", #required = "required" })
#Html.ValidationMessageFor(model => model.date)
</div>
Then I have edited my class with the annotation then same result here is my class. My date parameter in my database table which name is "contents" which I used in my view "x=>x.date"
public class Common
{
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime date { get; set; }
public class CommonModel
{
public content content{ get; set; }
}
}
Try This:
1) Put Data Annotation in your class like:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime date { get; set; }
2) Replacing this line:
#Html.TextBoxFor(x => x.date.ToString("MM-dd-yyyy"), new { #class = "date", #required = "required" })
for this should work:
#Html.EditorFor(x => x.date, new { #class = "date", #required = "required" })
I am using ASP.NET MVC 3 TextBoxFor in a form and would like to use type="email" for easier input for at least some mobile devices but cannot find how to set it using TextBoxFor. Is this not easily possible?
In View
#Html.LabelFor(m => m.Email)
#Html.TextBoxFor(m => m.Email)
In model
[StringLength(50)]
public string Email { get; set; }
(Already using a data annotation to protect size constraint in DB)
Try to use
#Html.TextBoxFor(m => m.Email, new { #type = "email" })
http://msdn.microsoft.com/en-us/library/ee703538.aspx (htmlAttributes)
You're using it the wrong way.
Use EditorFor in View:
#Html.LabelFor(m => m.Email)
#Html.EditorFor(m => m.Email)
In model, add the [DataType( DataType.EmailAddress )] Data Annotations property:
[DataType( DataType.EmailAddress )]
public string Email { get; set; }
Try adding [DataType( DataType.EmailAddress )] to the email property.
[DataType( DataType.EmailAddress )]
[StringLength(50)]
public string Email { get; set; }
[StringLength(50)]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
public string EmailAddress { get; set; }
Try to add this. I think it works.
Can the properties of a model be accessed indirectly in a Razor view?
So instead of:
#Html.LabelFor(model => model.ColA)
#Html.LabelFor(model => model.ColB)
#Html.LabelFor(model => model.ColC)
Is something like the following possible?
#foreach (var col in Model.Columns)
{
#Html.LabelFor(model => model[col])
}
I should qualify that the model to be used will be an EF model:
public class Record
{
[Key, Display(Name="Column A")]
public string ColA { get; set; }
[Display(Name="Column B")]
public string ColB { get; set; }
[Display(Name="Column C")]
public string ColC { get; set; }
}
public class RecordDbContext : DbContext
{
public DbSet<Record> Records { get; set; }
}
How might I implement 'Columns' to show the display name for the property?
If Model.Columns is a list that would almost work. You would need to change it to this though:
#foreach (var col in Model.Columns)
{
#Html.LabelFor(model => col)
}
Seems like your view model is not adapted to the requirements of your view (which is to loop through some values and display labels about them). You could use a collection in your view model instead of properties as it will allow you to loop:
public IEnumerable<SomeModel> Columns { get; set; }
Then in your view instead of writing a loop you could use a display template:
#model MyViewModel
#Html.DisplayFor(x => x.Columns)
and then define the corresponding template which will automatically be rendered for each element of the Columns collection (~/Views/Shared/DisplayTemplates/SomeModel.cshtml):
#model SomeModel
#Html.LabelFor(x => x.SomeModelProperty)
An article view model
public class ArticleViewModel : ViewModelBase
{
[Required(ErrorMessage = "Required")]
public string Title { get; set; }
[Required(ErrorMessage = "Choose the language")]
public BELocale Locale { get; set; }
}
public class BELocale : BEEntityBase
{
public string OriginalName { get; set; }
public string FriendlyName { get; set; }
public string TwoLetterISOName { get; set; }
}
A view "AddLocaleForArticle"
#model Models.ArticleViewModel
#using (Html.BeginForm("VefifyAddingLocaleForArticle", "Administration"))
{
#Html.TextBoxFor(m => m.Title, new { disabled = "disabled" })
#Html.DropDownListFor(m => m.Locale,
new SelectList(ViewBag.AvalaibleLocales, "ID", "OriginalName"), "Select a language"
)
#Html.ValidationMessageFor(m => m.Locale)
<input type="submit" value="Save" />
}
An action
public ActionResult VefifyAddingLocaleForPhoto(ArticleViewModel article)
{
//article.Locale == null for some reason.
//but article.Title isn't null, it contains the data
return RedirectToAction("AddingLocaleForPhotoSuccess", "adminka");
}
Why article.Locale is equal null and how to fix it?
When the form is submitted a dropdown list sends only the selected value to the controller. So you cannot expect it to populate an entire complex object such as BELocale using a dropdownlist. The best you could is to populate its ID property and fetch the remaining of the object from your data store using this id.
So you will have to modify your dropdownlist helper so that it is bound to the id property of the locale as first argument:
#Html.DropDownListFor(
m => m.Locale.ID,
new SelectList(ViewBag.AvalaibleLocales, "ID", "OriginalName"),
"Select a language"
)
Now inside the corresponding controller action you will get the id:
public ActionResult VefifyAddingLocaleForPhoto(ArticleViewModel article)
{
// article.Locale.ID will contain the selected locale id
// so you can use this information to fetch the corresponding BELocale object
...
}
You may fill dropdown like this in your view model
public List<KeyValuePair<int, String>> locale
{
get
{
return _localerepo.Findlocals().Select(x => new KeyValuePair<int, string>(x.ID, x.OriginalName)).ToList();
}
}
In your view use this
<%:Html.DropDownListFor(x => x.ID, new SelectList(Model.locale, "key", "value"), "--Select locale--")%>
<%= Html.ValidationMessageFor(model => model.ID)%>