Hi all,
I tried to search posts, without any result either, maybe I didn't use the right words.
I need a solution in MVC for validate the DropDownList value, populated from database using Model class and the Html.DropDownListFor Helper method and MySql.
In the view I have added new DDL and this is populated correctly from database
But when click at the submit
<div class="form-group">
<div class="col-md-offset-5 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
The error is
System.Web.Mvc.WebViewPage<TModel>.Model.get Object reference not set to an instance of an object error App_Web_sxdtrbqy
on
@Html.DropDownListFor(m => m.SelectedFruitCode, Model.Fruits, "[ === Please select === ]", new { @Class = "textarea" }) @Html.ValidationMessageFor(m => m.SelectedFruitCode, "", new { @class = "text-danger" })
Please, help me.
My code below
Complete View
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table>
<tr>
<td class="textarea">CL</td>
<td>
@Html.TextBoxFor(m => m.CL, new { @Class = "textarea", placeholder = "CL" })
@Html.ValidationMessageFor(m => m.CL, "", new { @class = "text-danger" })
</td>
<td class="textarea"></td>
<tr>
<td class="textarea">Ticket</td>
<td>
@Html.TextBoxFor(m => m.Ticket, new { @Class = "textarea", placeholder = "Ticket" })
@Html.ValidationMessageFor(m => m.Ticket, "", new { @class = "text-danger" })
</td>
<td class="textarea"></td>
<td class="textarea">Fruits</td>
<td>
@Html.DropDownListFor(m => m.SelectedFruitCode, Model.Fruits, "[ === Please select === ]", new { @Class = "textarea" })
@Html.ValidationMessageFor(m => m.SelectedFruitCode, "", new { @class = "text-danger" })
</td>
</tr>
</table>
<br />
<hr class="new3">
<div class="form-group">
<div class="col-md-offset-5 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")
<script type="text/javascript">
$(document).ready(function () {
$('input[type=datetime]').datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
yearRange: "-2:+0"
});
});
</script>
}
Complete Controller
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index(PersonModel person)
{
if (ModelState.IsValid)
{
string cl = person.CL;
string ticket = person.Ticket;
}
return View();
}
public class Fruit
{
public string Code { get; }
public string Name { get; }
public Fruit(string code, string name)
{
Code = code;
Name = name;
}
}
public class FruitsRepository
{
public List<Fruit> GetAll()
{
string sql;
var fruits = new List<Fruit>();
string constr = ConfigurationManager.ConnectionStrings["cnx"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
sql = @String.Format("SELECT * FROM `dotable`; ")
using (MySqlCommand cmd = new MySqlCommand(sql))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
var fruit = new Fruit(sdr["sCode"].ToString(), sdr["sName"].ToString());
fruits.Add(fruit);
}
}
con.Close();
}
}
return fruits;
}
}
[HttpGet]
public ActionResult Index()
{
var personModel = new PersonModel();
var fruitsRepo = new FruitsRepository();
var fruits = fruitsRepo.GetAll();
var fruitsSelecteListItems = fruits.Select(fruit => new SelectListItem
{
Text = fruit.Name,
Value = fruit.Code
}).ToList();
personModel.Fruits = fruitsSelecteListItems;
return View(personModel);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
Complete Models
public class PersonModel
{
[Required]
[Display(Name = "Fruits")]
public string SelectedFruitCode { get; set; }
public List<SelectListItem> Fruits { get; set; }
public string Namex { get; set; }
public string Codex { get; set; }
[Required]
[Display(Name = "CL")]
public string CL { get; set; }
[Required]
[Display(Name = "Ticket")]
public string Ticket { get; set; }
}