Helping user input correct format into EditFor - c#

My SQL database has a column Date which allows strings only in such format: __/____, basically month/year for example: 01/2019.
Is it possible for my:
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control" } })
to have some helper so that user can type only using the correct format? For example the editor which has strong typed something like this: __/___ and can only input numbers to fill underscores.

#Html.TextBoxFor(m => m.StartDate,
new { #Value = Model.StartDate.ToString("yyyy/MM/dd"), #class="datepicker" })

Related

Why isn't my EditorFor datepicker field populating my value?

I'm trying to get this datepicker field to populate with today's date but it's not happening for some reason. I can't find a reference for what options are accepted in the "new {}" section.
#Html.LabelFor(d => d.ServiceOn, new { #class = "control-label" })
#Html.EditorFor(d => d.ServiceOn, "DatePicker", new { disableMinDate = true, Value = DateTime.Today })
#Html.ValidationMessageFor(d => d.ServiceOn, "", new { #class = "text-danger" })
I've tried value, Value and #Value but the resulting html always shows value="". I'm wondering if maybe the datepicker itself is zeroing out the field. I feel like I'm missing something obvious here.
This should work:
#Html.LabelFor(d => d.ServiceOn, new { #class = "control-label" })
#{Model.ServiceOn= DateTime.Today;}
#Html.EditorFor(d => d.ServiceOn, "DatePicker", new { disableMinDate = true })
#Html.ValidationMessageFor(d => d.ServiceOn, "", new { #class = "text-danger" })
HTML work with the ModelState, not from the model itself.
If you want more loose helpers, ise #Html.Editor instead.

AJAX call from keydown on one text box to populate another text box ASP.NET MVC

I am attempting to populate a textbox from the value in a separate textbox, ie. using the customer account number to auto-populate the customer name. My AJAX is returning the proper results, however it's not getting populated into the other textbox, which is why I suspect my jQuery is wrong.
<div class="form-group">
#Html.LabelFor(model => model.AccountNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.AccountNumber, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.AccountNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CustomerName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CustomerName, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CustomerName, "", new { #class = "text-danger" })
</div>
</div>
[HttpGet]
public ActionResult GetCustomerName(string RecKey)
{
var result = (from c in db.Customers
where c.RecKey.Equals(RecKey)
select new { c.RecName });
return Json(result, JsonRequestBehavior.AllowGet);
}
Returned JSON (only returning one result that's an exact match):
[{"RecName":"This is a customer name"}]
$(document).ready(function () {
var RecKey;
$(function () {
$("#AccountNumber").keydown(function () {
RecKey = $("#AccountNumber").val();
$.ajax({
type: "GET",
url: 'GetCustomerName',
data: { RecKey: RecKey },
success: function (data) {
if (data) {
//alert(data);
$('#CustomerName').val(data.RecName);
}
}
});
});
});
});
I am rather new to jQuery and have had success with autocomplete on the same textbox, however my searching hasn't found much for me to populate other text boxes. Anyone have any ideas?
The issue is because the resulting JSON is an array of objects. If you can guarantee that it will only ever return a single object within that array then you can hard-code the index accessor of the array:
success: function (data) {
if (data) {
$('#CustomerName').val(data[0].RecName);
}
}
Alternatively you can amend your C# code to return a single entity instead of an array:
return Json(result.SingleOrDefault(), JsonRequestBehavior.AllowGet); // or FirstOrDefault()
I think the TextBox helpers only add the name attribute, while your JQuery code is looking for the box by ID....
Try changing;
#Html.TextBoxFor(model => model.CustomerName, new { #class = "form-control" })
to
#Html.TextBoxFor(model => model.CustomerName, new { #class = "form-control", id = "CustomerName" })
You will also need to use either the SingleOrDefault server side or [0] indexer client side as suggested in the other answers - because even though the array only has one item, it is still an array (not an object) so that will need fixed as well.
P.S. A useful tool in debugging stuff like this is pressing F12 to get the developer console up - you can then try typing your jquery code to see if it works directly into the console. As an example if you type $("#CustomerName") do you actually get an object returned? If not - then my answer is the reason why. If so - something else is affecting this.

MVC View - Date Format Issue

While Binding data to TextBox, I want to convert it to dd/MM/yyyy format. In the Following Code, HolidayDate can be in any format which comes from db.
#Html.TextBoxFor(m => m[i].HolidayDate, new { #class = "datepicker", #value = #Model[i].HolidayDate.ToString("dd/MM/yyyy") })
The date doesn't get converted to dd/MM/yyyy. How to Convert the date in a particular format?
#Html.TextBoxFor(m => m[i].HolidayDate, "{0:dd/MM/yyyy}", new { #class = "datepicker", #value = #Model[i].HolidayDate.ToString("dd/MM/yyyy") })

making a text field read only

I am using the code below to make the text entry read only but this does not become read only any idea why this could be
#Html.EditorFor(model => model.UserName, new { Readonly = "Readonly"})
Why cont you use
#Html.EditorFor(model => model.UserName, new { #readonly = true});
or you can use like
#Html.EditorFor(model => model.UserName, new { #readonly = "readonly"});
try textboxfor
#Html.TextBoxFor(model => model.UserName, new { #readonly = true})
The HtmlHelper EditorFor do not have overloads that take HTML attributes.
Instead of EditorFor go with TextBoxFor
#Html.TextBoxFor(model => model.UserName, new { disabled = "disabled", #readonly = "readonly"})
hope it helps

EditorFor AdditionalViewData not working

I have :
#Html.EditorFor(model => model.name)
This display my user name but I want to override this name. So I tried this :
#Html.EditorFor(model => model.Password, new { value = "othertext"} )
But this doesn't work, I always have my user name.
Thanks for your help !
The "additional view data" parameter on EditorFor sets up new view data to be used by your editor template(s). It does not make "extra objects" to be rendered as you seem to be using it here.
And as I understand you trying to set htmlAttributes:
#Html.TextBoxFor(model => model.Password, new { value = "xxx" })

Categories

Resources