convert text to UpperCase in mvc - asp.net

Hi I am trying to convert the text entered in textbox to uppercase, but I am not getting the output,please help.
#Html.TextBoxFor(model => model.PanNumber, new { #class = "form-control", #onkeyup = "InputToUpper(this);" })

This should work (remove #onkeyup = "InputToUpper(this);" from the TextBoxFor method)
$('#PanNumber').keyup(function() {
var text = $(this).val();
$(this).val(text.toUpperCase());
});

if you are using "#editorfor" text box then try
#Html.EditorFor(model => model.PanNumber.Alias, new {
htmlAttributes = new { #class = "upper-case" }
})

Related

ASP.NET MVC Dropdown Default value

Please look at my code. I am trying to load the default value for the drop-down. value is not accepting from the design. I'm loading data to the combobox from, the account controller, I have added list of items to the Tempdata and then in the design I'm assigning the list to the combo box. So here when loading the form I need to set the default value for the combo box drop down list. Please help
This is how I assigning values from the account controller.
List<Request_Types> RequestTyleList = db.Request_Types.Where(r => r.Status == true).ToList();
List<SelectListItem> ReqTypeDropDown = RequestTyleList.Select(r => new SelectListItem { Text = r.Request_Type, Value = r.Id.ToString() }).ToList();
TempData["RequestTyleList"] = ReqTypeDropDown;
This how I called that TempData in the view:
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
}
And this is where I assigning that values to the Combobox in the view
<div class="col-md-6 col-sm-6">
<div class="form-group row">
#Html.LabelFor(model => model.ReqType, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-sm-8">
#Html.DropDownListFor(model => model.ReqType, ReqTypes, new { #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.ReqType, "", new { #class = "text-danger" })
</div>
</div>
</div>
So I when opening the view, I need to assign the default value to the ReqTypes
Just add the following parameter to assign default value
#Html.DropDownListFor(model => model.ReqType, new SelectList(ReqTypes,"Value","Text","Your_Default_Value"), "Select",new { #class = "js-dropdown" })

How to set the selected value of an MVC5 DropDownList?

Given the following MVC5 code:
#Html.DropDownList(name: "Enhanced_Rijndael_AlgorithmID", optionLabel: null, selectList: EnhancedRijndaelAvailableHashAlgorithmList.Select(item => new SelectListItem
{
Value = item.RecordID.ToString(),
Text = item.HashAlgorithm,
Selected = "select" == item.RecordID.ToString()
}), htmlAttributes: new { #class = "form-control" })
Is it possible to set the "selected value" of this list?
The only thing I can think of is somehow setting the Selected value in the SelectListItem set.
Here is the what Ive come up with after being reminded of the DropDownListFor
#Html.DropDownListFor(expression: r => r.AlgorithmID, selectList: EnhancedRijndaelAvailableHashAlgorithmList.Select(item => new SelectListItem
{
Value = item.RecordID.ToString(),
Text = item.HashAlgorithm,
Selected = "select" == item.RecordID.ToString()
}),
htmlAttributes: new { #class = "form-control" }, optionLabel: "Algorithm")

Required Validation Not working For DropdownList in MVC

Below View Code. when i click on Create button without Selecting Activity. it is not showing validation message. i am using Required attribute in Model Class
<div class="form-group">
#Html.LabelFor(model => model.TransactionTypeID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.TransactionTypeID, new SelectList(ViewBag.GetT, "TransactionTypeID", "TransactionType"), "--Select--", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TransactionTypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ActivityID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.ActivityID, new SelectList(ViewBag.GetA, "ActivityID", "ActivityName"), "--Select--", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ActivityID, "", new { #class = "text-danger" })
</div>
</div>
//Controller code
public ActionResult Create()
{
List<TransactionTypeMaster> T = new DATransactionTypeMaster().GetListAll();
ViewBag.GetT = T;
List<ActivityMaster> A = new DAActivityMaster().GetListAll();
ViewBag.GetA = A;
return View();
}
Can Any One Help on this. Need To Know Where i m going Wrong
when i click on Create button without Selecting Activity. it is not showing validation message. i am using Required attribute in Model Class
try giving the default '--Select--' options in your drop downs a value of 0.
In your model, make sure your drop down value is set to '[Required]' and also give it the attribute:
[Range(1, Int32.MaxValue, ErrorMessage = "Must Select A Value")]
basically letting it only accept values > 0
I think you TransactionTypeID is int, make that Nullable that would fix this issue. Currently it doesn't work because by default it might be sending zero as a value by using null, it would send null when user doesn't touch this field.

ASP MVC Razor - FormValidation - #Html.ValidationMessageFor - Editor [duplicate]

This question already has an answer here:
attaching jquery validation to replacement element
(1 answer)
Closed 6 years ago.
Validation working on all other elements i used sofar but validation-message for editor does not show up. ModelState has correct error-state,
but no message can be seen on the form.
here is what i have:
MODEL:
[Required(AllowEmptyStrings = false, ErrorMessage = "Diese Feld muss angegeben werden.")]
[StringLength(8000,ErrorMessage = "Feld muss zwischen {2} und {1} Zeichen enthalten.", MinimumLength = 8)]
[DataType(DataType.MultilineText)]
[AllowHtml]
[Display(Name = "Beschreibung")]
public string Description { get; set; }
VIEW:
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
this also does not work:
#Html.ValidationMessageFor(model => model.Description)
when i do this in view:
#Html.ValidationMessageFor(model => model.Description, "test message")
it works - meaning, it shows "test message" - but i want the messages i defined in model...
---- here is some field that works as expected ----
MODEL:
[Required(ErrorMessage = "Dieses Feld muss angegeben werden.")]
[StringLength(25, MinimumLength = 2)]
[Display(Name = "KeyWord")]
public string Keyword { get; set; }
VIEW:
<div class="form-group">
#Html.LabelFor(model => model.Keyword, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Keyword, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Keyword, "", new { #class = "text-danger" })
</div>
</div>
when field is empty and submit attempted, the required message is shown.
when field contains too-less or too many characters, and field is not in focus or submit is attempted, string-length message is shown ..
that's what i would like for the 2nd editor ...
----- end of working field -----
what am i doing wrong ?
thank you
SOLVED:
it was that "jQuery TE" was used to beautify the editor.
thank you #Stephen Muecke and #Varun Vasishtha for pushing me there ;)
There must be a javascript error that is restricting jquery validation to work, try to debug using Firebug in Firefox
Exclude the last "test message":
#Html.ValidationMessageFor(model => model.Description)
See the documentation: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationmessagefor%28v=vs.118%29.aspx

Html.SelectFor razor syntax add class

I want to add a class to a selectfor
but I get an error
#Html.SelectFor(m => m.foo, optionLabel: null, new { #class = "foo12" })
With a text box it works:
#Html.TextBoxFormattedFor(m => m.foo, new { #class = "foo" })
Error I get:
Named argument specifications must appear after all fixed arguments have been specified.
The error is self-explanatory -- any named arguments (in this case "optionLabel") have to come after unnamed ones. So instead of this:
#Html.SelectFor(m => m.foo, // 1
optionLabel: null, // 2
new { #class = "foo12" } // 3
)
I guess you probably want this:
#Html.SelectFor(m => m.foo, // 1
optionLabel: null, // 2
htmlAttributes: new { #class = "foo12" } // 3
)
Edit
Surely you mean DropDownListFor, and not "SelectListFor"? You need to provide the options as well. Something like this:
#{
var selectList = new SelectListItem[]
{
new SelectListItem { Text = "text", Value = "value" },
};
}
#Html.DropDownListFor(m => m.foo,
selectlist: selectlist,
htmlAttributes: new { #class = "foo" }
)

Resources