I had created a form with 2 dropdownlists and a text box. I am populating values for htmldropdownlist through tempdata.I am not using model class in the view.
Is there a way to validate the form if dropdownlist selected / if textbox is blank without javascript /jquery.
I know we can validate the view when using model class through "required" attribute. But is there a way to validate without models.
According to your needs, I made an example, please refer to it.
Controller
public ActionResult Index()
{
List < SelectListItem > itemList = new List<SelectListItem>
{
new SelectListItem()
{
Value ="1",
Text = "TEST1"
},new SelectListItem()
{
Value ="2",
Text = "TEST2"
},
};
SelectList list = new SelectList(itemList, "Value", "Text");
TempData["Dropdownlist"] =list;
return View();
}
[HttpPost]
public ActionResult ValidationTest()
{
//get the selected value
var DropDownListTest = Request["DropDownListTest"];
if (DropDownListTest == null)
{
//do something
}
var text1 = Request["text1"];
if (text1 == null)
{
//do something
}
return RedirectToAction("Index");
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I assume this approach makes a postback to the server. Is there a way to validate in browser side (client side) itself without making a postback and also by not using jquery / javascript.
Is there a way to validate in client side itself by using Razor.
First of all, if you want to implement client-side Validation, almost all will use js or jquery.
Second, MVC client Validation must refer to the JavaScript library, but you do not need to write js yourself. If you can accept references to js files, you can refer to this
link to implement MVC client Validation.(This method will use the model.)
Remarks: I think this link can better help you understand the validation in MVC.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
43 Posts
Regarding ASP.Net MVC HtmlDropdownlist validation
May 28, 2020 01:10 PM|ashvinvee|LINK
Hi,
I am new to asp.net mvc.
I had created a form with 2 dropdownlists and a text box. I am populating values for htmldropdownlist through tempdata.I am not using model class in the view.
Is there a way to validate the form if dropdownlist selected / if textbox is blank without javascript /jquery.
I know we can validate the view when using model class through "required" attribute. But is there a way to validate without models.
Thanks, Ashvin
Contributor
2770 Points
793 Posts
Re: Regarding ASP.Net MVC HtmlDropdownlist validation
May 29, 2020 02:57 AM|YihuiSun|LINK
Hi, ashvinvee
According to your needs, I made an example, please refer to it.
Controller
Index
Here is the result.
Best Regards,
YihuiSun
Member
6 Points
43 Posts
Re: Regarding ASP.Net MVC HtmlDropdownlist validation
May 29, 2020 06:19 AM|ashvinvee|LINK
Hi YihuiSun,
Thanks for your inputs.
I assume this approach makes a postback to the server. Is there a way to validate in browser side (client side) itself without making a postback and also by not using jquery / javascript.
Is there a way to validate in client side itself by using Razor.
Please suggest
Thanks,
Ashvin
Contributor
2770 Points
793 Posts
Re: Regarding ASP.Net MVC HtmlDropdownlist validation
May 29, 2020 07:08 AM|YihuiSun|LINK
Hi, ashvinvee
First of all, if you want to implement client-side Validation, almost all will use js or jquery.
Second, MVC client Validation must refer to the JavaScript library, but you do not need to write js yourself. If you can accept references to js files, you can refer to this link to implement MVC client Validation.(This method will use the model.)
Remarks: I think this link can better help you understand the validation in MVC.
Best Regards,
YihuiSun