Answered by:
Regarding ASP.Net MVC HtmlDropdownlist validation

Question
-
User-1961814522 posted
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
Thursday, May 28, 2020 1:10 PM
Answers
-
User1686398519 posted
Hi, ashvinvee
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"); }
Index
@{ Layout = null;} @using (Html.BeginForm("ValidationTest","Home")) { @Html.DropDownList("DropDownListTest", TempData["Dropdownlist"] as SelectList, "Please select") @Html.TextBox("text1") <button type="submit">submit</button> }
Here is the result.
Best Regards,YihuiSun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 29, 2020 2:57 AM
All replies
-
User1686398519 posted
Hi, ashvinvee
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"); }
Index
@{ Layout = null;} @using (Html.BeginForm("ValidationTest","Home")) { @Html.DropDownList("DropDownListTest", TempData["Dropdownlist"] as SelectList, "Please select") @Html.TextBox("text1") <button type="submit">submit</button> }
Here is the result.
Best Regards,YihuiSun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 29, 2020 2:57 AM -
User-1961814522 posted
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
Friday, May 29, 2020 6:19 AM -
User1686398519 posted
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
Friday, May 29, 2020 7:08 AM