Lalith Prasad

Lalith Prasad

  • NA
  • 17
  • 532

Modelstate Data

Oct 16 2019 6:54 AM
How to get ModelState Values in the ActionMethod of the Controller?
Here's my Index View Code -
 
  1. Here's my Index View Code -   
  2. @using (Html.BeginForm("Index""Home"))  
  3. {  
  4. @Html.LabelFor(p => p.name, "Name: ")  
  5. @Html.TextBoxFor(p => p.name)  
  6.   
  7. @Html.LabelFor(p => p.height, "Height: ")  
  8. @Html.TextBoxFor(p => p.height)  
  9.   
  10. @Html.LabelFor(p => p.number, "Id: ")  
  11. @Html.TextBoxFor(p => p.number)  
  12.   
  13. @Html.LabelFor(p => p.typeId, "Type : ")  
  14. @Html.DropDownList("typeId", TempData["typeId"] as SelectList)  
  15.   
  16. @Html.LabelFor(p => p.isLegendary, "Is Legendary")  
  17. @Html.CheckBoxFor(p => p.isLegendary)  
  18.   
  19. "submit" name="btnSumit" value="Submit" />  
  20. }  
 My Code in Controller -
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using WebApplication1.Models;  
  7.   
  8. namespace WebApplication1.Controllers  
  9. {  
  10.   
  11.     public class HomeController : Controller  
  12.     {  
  13.         public ActionResult Index()  
  14.         {  
  15.             List<PokemonType> pokeTypes = new List<PokemonType>() {  
  16.                 new PokemonType(){typeId = 1,pokeType = "Fire"},  
  17.                 new PokemonType(){typeId = 2,pokeType = "Water"},  
  18.                 new PokemonType(){typeId = 3,pokeType = "Thunder"},  
  19.                 new PokemonType(){typeId = 4,pokeType = "Dark" }  
  20.             };  
  21.             Pokemon p = new Pokemon();  
  22.             SelectList sl = new SelectList(pokeTypes, dataValueField: "typeId", dataTextField: "pokeType", selectedValue: 4);  
  23.             TempData["typeId"] = sl;  
  24.             TempData.Keep();  
  25.             //TempData.Keep();  
  26.             return View(p);  
  27.         }  
  28.   
  29.         [HttpPost]  
  30.         public string Index(Pokemon p)  
  31.         {  
  32.             string isLegendary = "";  
  33.             if (p.isLegendary)  
  34.             {  
  35.                 isLegendary = "Yes";  
  36.             }  
  37.             else  
  38.             {  
  39.                 isLegendary = "No";  
  40.             }  
  41.   
  42.             //ModelState.Clear();  
  43.             var type = ViewData.ModelState["typeId"].Value;  
  44.             var name = ViewData.ModelState["name"].Value;  
  45.             string s = $"Name: {name}  <br>Height: {p.height} cm <br>Id: #{p.number}  <br>Type: {type}  <br>Is Legendary: {isLegendary}";  
  46.             return s;  
  47.         }  
My Model Code -
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace WebApplication1.Models  
  7. {  
  8.     public class PokemonType  
  9.     {  
  10.         public int typeId { getset; }  
  11.         public string pokeType { getset; }  
  12.   
  13.         public override string ToString()  
  14.         {  
  15.             return pokeType;  
  16.         }  
  17.     }  
  18.     public class Pokemon  
  19.     {  
  20.         public string name { getset; }  
  21.         public float height { getset; }  
  22.         public int number { getset; }  
  23.         //public PokemonType type { get; set; }  
  24.         public int typeId { getset; }  
  25.         public bool isLegendary { getset; }  
  26.     }  
With the Above code i get Output as -
 
Name:System.Web.Mvc.ValueProviderResult
Height: 99.7 cm
Id: #197
Type: System.Web.Mvc.ValueProviderResult
Is Legendary: Yes
 
Second method which i have tried is using Extension Method -
 
  1. public static string DisplayType(this SelectList selectList)  
  2. {  
  3.     return selectList.DataTextField;  
  4. }  
and changing string in [HttpPost] Index method -
 
  1. string s = $"Name: {p.name}  <br>Height: {p.height} cm <br>Id: #{p.number}  <br>Type: {HelperClassExtension.DisplayType(TempData["typeId"] as SelectList)}  <br>Is Legendary: {isLegendary}";  
  2.            return s;  
With These Changes, My output is -
 
Name:Umbreon
Height: 99.7 cm
Id: #197
Type: pokeType
Is Legendary: No 
 

So, I get pokeType instead of value Dark. (i've selected dark from the list.)

These are the things I have tried to retrieve the value of type(fire,water,thunder and dark). so, is there any way through which I can retrieve it? 


Answers (1)