Both Date & Time EditorFor editor in mvc razor? - asp.net-mvc

When using #Html.EditorFor thing for date or time in mvc razor one can define a model like
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[DataType(DataType.Time)]
or
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
however, when I change the syntax to DataType.DateTime, in my view I don't get datepicker or time mask with spinner, it just gives me a blank textbox. In my view I need something like dd/mm/yyyy hh:mm posting to the database. Is there a way to do it in mvc razor without using bootstrap datetimepicker/jquery-ui? I am using MVC5.

Answer for 2022.
Try using the default elements from HTML5. Add #type = "date" and #type = "date" to the htmlAttributes
<div class="row">
<div class="col-12 col-md-6">
#Html.LabelFor(Model => Model.TheDate)
#Html.EditorFor(Model => Model.TheDate, new { htmlAttributes = new { Name = "TheDate", #class = "form-control", #type = "date" } })
#Html.HiddenFor(Model => Model.TheDate)
</div>
<div class="col-12 col-md-6">
#Html.LabelFor(Model => Model.TheTime)
#Html.EditorFor(Model => Model.TheTime, new { htmlAttributes = new { Name = "TheTime", #class = "form-control", #type = "time" } })
#Html.ValidationMessageFor(Model => Model.TheTime, "", new { #class = "text-danger" })
</div>
</div>
What you´re doing here is adding the correct types (type="date", and type="time") to the HTML elements. This is the rendered result:
<div class="row">
<div class="col-12 col-md-6">
<label for="TheDate">Date</label>
<input Name="TheDate" class="form-control text-box single-line" data-val="true" data-val-required="Enter the correct date." id="TheDate" name="TheDate" type="date" value="2022-08-18" />
<span class="field-validation-valid text-danger" data-valmsg-for="TheDate" data-valmsg-replace="true"></span>
</div>
<div class="col-12 col-md-6">
<label for="TheTime">Time</label>
<input Name="TheTime" class="form-control text-box single-line" data-val="true" data-val-required="Enter the correct time." id="TheTime" name="TheTime" type="time" value="10:00" />
<span class="field-validation-valid text-danger" data-valmsg-for="TheTime" data-valmsg-replace="true"></span>
</div>
</div>
What you get in the interface (using Google Chrome v104):
and

Related

How to convert a static html into razor view in asp.net mvc?

I have the following simple bootstrap form:
#{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Register</h3>
<br>
<form action="/User/Register">
<div class="form-group row">
<label class="col-sm-2 col-md-1 col-form-label">Name</label>
<div class="col-sm-10 col-md-2">
<input name="name" type="text" class="form-control" placeholder="Enter Name">
</div>
</div>
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-md-1 col-form-label">Email</label>
<div class="col-sm-10 col-md-2">
<input name="email" type="email" class="form-control" placeholder="Enter email">
</div>
</div>
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-md-1 col-form-label">MAC</label>
<div class="col-sm-10 col-md-2 positioned_relative">
<span class="add-new-icon glyphicon glyphicon-plus-sign" id="add_mac"> </span>
<input type="text" class="form-control" id="mac_addr" placeholder="Enter MAC">
</div>
</div>
<div class="form-group row new_mac_wrapper">
<div class="col-md-offset-3">
<div class="new_mac_container">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary col-sm-offset-1">Register</button>
</form>
I want to convert this into razor view as follows:
#model RouterManagement.Models.UserViewModel
#{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3 class="pull-left">Register</h3> <br />
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group row">
#Html.LabelFor(model => model.Name, htmlAttributes: new { #class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-2">
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-2">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Password, htmlAttributes: new { #class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-2">
#Html.EditorFor(model => model.Password, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Mac, htmlAttributes: new { #class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-2">
#Html.EditorFor(model => model.Mac, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mac, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-sm-10 col-md-2">
<input type="submit" value="Create" class="btn btn-primary col-sm-offset-1" />
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
And for this conversion I get the following rendered HTML:
<form action="/Users/Register" method="post"><input name="__RequestVerificationToken" type="hidden" value="IZ_dvCy7QtB1VsoqQGh6x_Yzr1DME9V6LjKs1Fi8KL6KxOoKNNvFlH6mdw8yD4xIj-LKaUXFsNZndDTeHOa8xCVZPdu7b8qNXeL05IdIyiQ1"> <div class="form-group row">
<label class="col-sm-2 col-md-1 col-form-label" for="Name">Name</label>
<div class="col-sm-10 col-md-2">
<input class="form-control text-box single-line" id="Name" name="Name" type="text" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="Name" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-md-1 col-form-label" for="Email">Email</label>
<div class="col-sm-10 col-md-2">
<input class="form-control text-box single-line" id="Email" name="Email" type="text" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-md-1 col-form-label" for="Password">Password</label>
<div class="col-sm-10 col-md-2">
<input class="form-control text-box single-line" id="Password" name="Password" type="text" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="Password" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-md-1 col-form-label" for="Mac">Mac</label>
<div class="col-sm-10 col-md-2">
<span class="field-validation-valid text-danger" data-valmsg-for="Mac" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-sm-10 col-md-2">
<input type="submit" value="Create" class="btn btn-primary col-sm-offset-1">
</div>
</div>
</form>
This is approximately close to my static html. If you pay a deep attention you can see some additional html generated in case of razor view. I am okay with this additional html but why my design has been broken. I checked for css and js files. All are okay, but there is something related to razor conversion only. Any Idea?
N.S: I am switching from PHP to ASP. So I am apologizing if my question is not standard to ask here. Thanks for your time.
In brief - this two HTML's are not the same.
They differ in a few things:
- in original bootstrap form, you have inputs for Name, Email, and Mac - in generated you have Name, Email, Password, and Mac.
Then in original, each input tag is wrapped in div with class = "form-group row" in generated only first input has these, and the rest have only "form-group". Then in original inside MAC input group, you have div with class="col-sm-10 col-md-2 positioned_relative" and in original, there is no such thing.
Also in original, you have div with class="form-group row new_mac_wrapper" and in the generated code there is none.
I will not go further into details but as you can see you have two different code - hard to tell what is breaking things without a chance to see the whole project.
However, the suggestion is to start little by little - start with just one field (Name) in original and on generated code.
See the differences and only when there is none keep up adding and comparing one field at the time.

How to prevent asp mvc generating extra code on EditorForModel for every property?

I have created an EditorTemplate for multiline text data types.
But after render page, ASP adds extra html code. How do I prevent this?
My EditorTemplate for multiline text is:
#model string
<div class="control-group">
#Html.LabelFor(x => x, new {#class="control-label col-md-2"})
<div class="col-md-10">
#Html.TextAreaFor(x => x , new { #class="form-control"})
#Html.ValidationMessageFor(x => x, "", new { #class = "text-danger" })
</div>
</div>
generated code is:
<div class="editor-label">
<label for="Description">ِDescription</label>
</div>
<div class="editor-field">
/*Start My template*/
<div class="control-group">
<label class="control-label col-md-2" for="Description">Description</label>
<div class="col-md-10">
<textarea class="form-control" cols="20" id="Description" name="Description" rows="2"></textarea>
<span class="field-validation-valid text-danger" data-valmsg-for="Description" data-valmsg-replace="true"></span>
</div>
/*End of template*/
</div>
<span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="true"></span></div>

object name is bind as prefix in input control

currently I am working in asp.net mvc view
I create form using below code.
#{
T4.Models.Order o = new T4.Models.Order();
}
#using (Html.BeginForm("Create", "Default5", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Order</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => o.CustomerID, new {#class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model=>o.CustomerID)
#Html.ValidationMessageFor(model => o.CustomerID)
</div>
</div>
</div>
}
this code render perfectly but problem is it render with prefix of object's name.
show below html code
<input class="text-box single-line" data-val="true" data-val-required="The CustomerID field is required." id="o_CustomerID" name="o.CustomerID" type="text" value="">
you can see this html input control has id="o_CustomerID".I want that it only render with id="CustomerID".
I does not know why it takes object name as prefix.
please help me.
Thank You.
No need to initialize object of T4.Models.Order class.
#{
T4.Models.Order o = new T4.Models.Order();
}
Replace above code to:
#model T4.Models.Order
Then simple use:
<div class="form-group">
#Html.LabelFor(model => model.CustomerID, new {#class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model=> model.CustomerID)
#Html.ValidationMessageFor(model => model.CustomerID)
</div>
</div>

How to set first checkbox checked in mvc

Here i want to set the first checkbox checked.I am not understanding how,since its in a viewbag.
<div class="form-group">
#Html.LabelFor(m => m.RBO_Location, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#foreach (var item in (SelectList)ViewBag.Branches)
{
<div>
<input type="checkbox" name="selectedBranches" value="#item.Value" class="checkbox-inline" />
#Html.Label(item.Text, new { #class = "control-label" })
</div>
}
</div>
</div>
How to set first checkbox checked in mvc
This can be done using Jquery at Document.Ready.
$("input[type=checkbox][name=selectedBranches]:first").attr('checked','checked');

How to bind input field to editfor model MVC

I have the following javascript and input fields within my model.
script type="text/javascript">
function sum() {
var txtFirstNumberValue = document.getElementById('money1').value;
var txtSecondNumberValue = document.getElementById('money2').value;
var txtThirdNumberValue = document.getElementById('money3').value;
var result = parseFloat(txtFirstNumberValue) + parseFloat(txtSecondNumberValue) + parseFloat(txtThirdNumberValue);
if (!isNaN(result)) {
document.getElementById('Total').value = result;
}
}
</script>
<div class="form-group">
#Html.LabelFor(model => model.Total, "Total", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
<input type="text" id="Total" name="Total" />
#*#Html.EditorFor(model => model.Total)*#
#Html.ValidationMessageFor(model => model.Total)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.money1, "money1", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
#*#Html.EditorFor(model => model.money1)*#
<input type="text" id="money1" onkeyup="sum();" name="money1" />
#Html.ValidationMessageFor(model => model.money1)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.money2, "money2", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
#*#Html.EditorFor(model => model.money2)*#
<input type="text" id="money2" onkeyup="sum();" name="money2" />
#Html.ValidationMessageFor(model => model.money2)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.money3, "money3", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
#*#Html.EditorFor(model => model.money3)*#
<input type="text" id="money3" onkeyup="sum();" name="money3" />
#Html.ValidationMessageFor(model => model.money3)
</div>
</div>
This all works fine. When I type a value into the 3 money fields the resulting value appears in the Total field. Within MVC if I click on the details view it shows all four values, however if I click on the edit view all four fields are blank. Question is how do I get the values to appear and remain in edit mode?
Well, for starters your model-bound input fields are commented out:
#*#Html.EditorFor(model => model.money1)*#
In order to get those to work you'd need to un-comment them.
You could just manually populate your markup with the model values, something like this:
<input type="text" id="money1" onkeyup="sum();" name="money1" value="#model.money1" />
That should pre-populate that input with the model's money1 value. Though you're assuming the responsibility of manually implementing anything else that the built-in HTML helpers would provide for you. Unless there's a compelling reason not to use Html.EditorFor() I imagine that would be the better option.

Resources