ASP.NET MVC Model Binders, html id - asp.net-mvc-2

Using the Model Binders in ASP.NET MVC 2.0, you can do something like this...
[DisplayName("User Name")]
public string Name
{
get;
set;
}
<%: Html.TextBoxFor( m => m.Name ) :%>
and then in your HTML, you get a result like this..
<label for="UserName">User Name</label>
<input type="text" id="UserName" name="UserName" />
That works fine, but I want to have better control over the HTML ID. Is there any way to do this through the model binding method?

You need to override the editor template for a string to control how the editor for a string is rendered. This basically involves creating a String.ascx partial view. You can find more detailed information in the "Overriding Templates" section of this blog post.

Related

Is it possible to not encode the Label tag value rendered in ASP.NET Core HtmlHelper?

I asked a similar question a while back:
Is it possible to format Display attribute's Name property value of MVC Model class?
I am wondering would it be possible to not apply encoding in the ASP.NET Core HtmlHelper.Label tag?
What I have now is:
<label asp-for="CorporationName" class="control-label"></label>
I understand it will render the html .
Like so:
<label class="control-label" for="CorporationName">Corporation / <br/>Entreprise</label>
with the text value defined in this model class:
[DataType(DataType.Text)]
[Display(Name = "Corporation / <br/> Enterprise")]
public string Corporation { get; set; }
So I am wondering if it is possible to apply something similar to #Html.Raw to the asp-for attribute value in the label tag above so it will show as:
Corporation /
Enterprise
Or I need to write my own label class so it will do the encoding?
Thank you.
I think your requirement can be known as, you want to use tag helper and display [Display(Name = "Corporation / Enterprise")] to display as Corporation / </br> Enterprise.
Basically, you are trying to change the html content appearance, and you want to realize it via taghelper, but actually there's no such feature, you may try to create a custom taghelper like this answer. But I think it's too complex.
In my humble opinion, you can modify the html content by js. But the easiest way to add linebreaker into html content is </br>, so my test code looks like below, it worked for me:
<div>
<label id="CorporationLabor" asp-for="Corporation" class="control-label"></label>
<input asp-for="Corporation" class="form-control" />
</div>
#section Scripts{
<script>
$(function(){
$("#CorporationLabor").html("Corporation / </br> Enterprise!!!");
});
</script>
}

ASP.Net Core MVC date input value

I created a edit view for a EF Model and have a problem with the datepicker.
Whenever i try to edit a `dataset, the edit view don't show me the old date values.
This is my Edit.cshtml Code:
<div class="form-group">
<label asp-for="BestellungsDatum" class="control-label"></label>
<input asp-for="BestellungsDatum" type="date" class="form- control" value="#Model.BestellungsDatum" />
<span asp-validation-for="BestellungsDatum" class="text-danger"></span>
</div>
This is what it looks like, but there have to be the old value at this position:
When i change the input to type="text", the value is shown but there is no datepicker.. Any solutions on this?
type="date" generates the browsers HTML-5 datepicker if supported. The specifications require that the value be formatted in ISO format (yyyy-MM-dd) which is then displayed in accordance with the browsers culture.
Add the asp-format to format the value, and remove the value attribute (Tag Helpers correctly generate the value attribute for correct 2-way model binding and you should never attempt to add it yourself)
<input asp-for="BestellungsDatum" type="date" asp-format="{0:yyyy-MM-dd}" class="form-control" />
Note the HTML-5 datepicker is not supported in IE, and only in recent versions of FireFox. If you want a consistent UI, then consider using a jQuery datepicker instead.
Just like to add an addition to the accepted answer:
<input asp-for="BestellungsDatum" type="date" class="form-control" />
Rather than adding asp-format to the view, add DataFormatString to the data Model or ViewModel, so you don't have to add it to every view that uses it:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime BestellungsDatum { get; set; }

Modify Wicket FormComponent markup without panel?

In Wicket, I'd like to subclass TextField form component to add additional markup around the tag.
Why I do not want to create a Panel:
1) I want the web page designer to use the input tag:
<input wicket:id="blah">
2) I don't want the subclass to lose the FormField semantics in Java, e.g.:
field.setRequired(true);, etc.
I'm fine with hard-coding the wrapping markup in Java. But I'd like this to behave like a FormField in Java.
Any ideas? Looked around for examples, but stumped on this one
Edit:
I'm aware of Borders, but my issue with them is you have to add them in both the markup and in Java. For example:
<div wicket:id="border">
<input type="text" wicket:id="field"/>
</div>
--
FormComponent<Integer> field = new TextField<Integer>("field", new Model(1));
field.setRequired(true);
Border border = new MyBorder("border");
border.add(field);
form.add(border);
This makes the web page designer have to be aware of special markup, and the Java can't be encapsulated (as a FormField subclass).
Ah, this is what I wanted via IBehavior:
My wrapper behavior (sorry for the Scala syntax):
class FieldWrapper extends AbstractTransformerBehavior {
def transform(component: Component, output: CharSequence): CharSequence = """
<div class="blah">
Blah blah blah
%s
</div>
""".format(output)
}
My subclass:
class MyField[T](id: String, model: IModel[T]) extends TextField[T](id, model) {
add(new FieldWrapper)
}
Original Markup:
<input type="text" wicket:id="foobar"/>
Generated markup:
<div class="blah">
Blah blah blah
<input type="text" value="" name="foobar" xmlns:wicket="http://wicket.apache.org">
</div>
Thanks S.O. for jumpstarting my mind :-)
You wouldn't even need to subclass TextField. although it might be easier to to so if you want to reuse it. If you just want to add markup outside of the original tag, it's the poster use case for a Border.
If digging into the rendering of a Component is needed
MarkupContainer#onRender()
is your friend.
An example might be:
AbstractTree#onRender()
mf

validation without strongly typed views

How to perform validation on server side, if I am not creating strongly typed views. Just plain views with
<input id="in1" name="in1" value="" />
<input id="in2" name="in2" value="" />
<input id="in3" name="in3" value="" />
This will depend on what framework you are using for validation on your server side. Data Annotations work by decorating your view model classes with validation attributes. If you don't use view models (which would be very bad design) you could always perform the validation manually (which would be very bad design):
// NEVER WRITE CODE LIKE THIS! USE VIEW MODELS
[HttpPost]
public ActionResult Index(string int1, string int2, string int3)
{
if (string.IsNullOrEmpty(int1))
{
ModelState.AddModelError("int1", "int1 is required");
}
....
}

ASP.NET MVC 2: What Model property datatype will autobind an html select (DDL) with multiple selections?

The customer has a Model property that requires a comma separated list of selected options. We present their select list (DDL) as a multi-choice drop down.
What would the property datatype look like that would autobind multi-selections in the client side HTML select (DDL)?
The select posts data like this:
myOptions=Volvo&myOptions=Mercedes&myOptions=Audi
And we want to automagically bind it back to some property:
IList<string> CarChoices {get;set;}
So the POST action method parameter would be (Carform myForm)
which would have myForm.CarChoices which includes a List of the three selected cars?
I might be misunderstanding what you're trying to accomplish but I think this post from Phil Haack describes how to do what you're attempting to do in a clean way: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Sometimes it is just easier to get your hands dirty and work with the HTML. I suggest doing something like this:
<select multiple>
<% foreach(var item in Model){ %>
<option value="<%= item.ID %>"><%= item.Description %></option>
<% } %>
</select>
obviously your model is your collection. You can also use the ViewData["Whatever"] object to pass data as well, your choice.