when i submitting form then error is coming.
System.InvalidOperationException: 'The ViewData item that has the key 'U_ID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.'
View
@model Account_App.Models.InvestmentMV
@{
ViewBag.Title = "EntryFrm";
}
<body>
<div class="col-lg-8">
<div class="card card-default mb-5">
<div class="card-header" style="font-size:x-large">Entry Form</div>
<div class="card-body">
@Html.ActionLink("Create New", "EntryFrm", null, new { @class = "btn btn-primary" })
@using (Html.BeginForm("EntryFrm", "Accounts", FormMethod.Post, new { @target = "_blank" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@*@Html.HiddenFor(u => u.Prdno)*@
<div class="form-group">
@Html.LabelFor(model => model.U_ID, "Select Item", htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.DropDownList("U_ID", (SelectList)ViewBag.U_ID, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
@Html.ValidationMessageFor(model => model.U_ID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Inv_Amount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Inv_Amount, new { htmlAttributes = new { @class = "form-control", @id = "item_Weight" } })
@Html.ValidationMessageFor(model => model.Inv_Amount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "AllDesignation")
</div>
</div>
</div>
</div>
</body>
Controller
public ActionResult EntryFrm()
{
var u_ID = 0;
ViewBag.U_ID = new SelectList(DB.tbl_Invester_Name.Where(bt => bt.U_ID > u_ID), "U_ID", "U_Name", "0");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EntryFrm(InvestmentMV investmentMV)
{
var newinvestment = new tbl_Invesment();
newinvestment.U_ID = investmentMV.U_ID;
newinvestment.Inv_Amount = investmentMV.Inv_Amount;
newinvestment.E_Date = DateTime.Now;
DB.tbl_Invesment.Add(newinvestment);
DB.SaveChanges();
return View(investmentMV);
}
Model Class
public class InvestmentMV
{
public int U_ID { get; set; }
public string U_Name { get; set; }
[Display(Name = "Amount")]
public int Inv_Amount { get; set; }
public List<tbl_Invester_Name> Invester { get; set;}
}