When I fill in the information on my add product page and try to save, I get the error.
public ActionResult Create()
{
List<SelectListItem> values= (from i in db.Categories.ToList()
select new SelectListItem
{
Text = i.Name,
Value = i.Id.ToString()
}
).ToList();
ViewBag.val = values;
return View();
}
View
<div class="form-group">
#Html.LabelFor(model => model.CategoryId, "CategoryId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(m=> m.CategoryId ,(List<SelectListItem>)ViewBag.val, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CategoryId, "", new { #class = "text-danger" })
</div>
</div>
Error
System.InvalidOperationException: 'The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable'.'
ViewBag doesn't require type-casting, try changing your code in View from
#Html.DropDownListFor(m=> m.CategoryId ,(List)ViewBag.val, new { #class = "form-control" })
to
#Html.DropDownListFor(m=> m.CategoryId ,#ViewBag.val, new { #class = "form-control" })
Or, you may also refer this SO answer
and use something like below -
#Html.DropDownListFor(m => m.ContribType,
new SelectList(#ViewBag.ContribTypeOptions, "ContribId",
"Value", Model.ContribTypeOptions.First().ContribId),
"Select, please")
Related
I have a Course Table and a Hole Table in SQL. There is a foreign Key in the hole Table that is set to the CourseId.
This is so that when I create a hole I can assign it to the relevant course.
In my View I have a dropdownlistfor that has a list of courses to select from when creating the hole. However when I try and post back the values the courseID does not post back.
HoleViewModel
// GET: HoleViewModels/Create
public ActionResult Create()
{
var dbcourse = db.Course.ToList();
//Make selectlist, which is IEnumerable<SelectListItem>
var courseNameDropdownList = new SelectList(db.Course.Select(item => new SelectListItem()
{
Text = item.CourseName.ToString(),
Value = item.CourseId.ToString()
}).ToList(), "Value", "Text");
// Assign the Selectlist to the View Model
var viewCourse = new HoleViewModel()
{
Course = dbcourse.FirstOrDefault(),
// The Dropdownlist values
CourseNamesDropdownList = courseNameDropdownList,
};
return View(viewCourse);
}
// POST: HoleViewModels/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "HoleId,HoleNumber,Par,Length,StrokeIndex,CourseId")] HoleViewModel holeViewModel)
{
if (ModelState.IsValid)
{
db.HoleViewModels.Add(holeViewModel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(holeViewModel);
}
The Create.cshtml
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>HoleViewModel</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.HoleNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.HoleNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.HoleNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Par, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Par, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Par, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Length, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Length, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Length, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.StrokeIndex, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.StrokeIndex, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.StrokeIndex, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Course.CourseId, "CourseId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.Course.CourseId, Model.CourseNamesDropdownList, "Please select from List", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CourseId, "", new { #class = "text-danger" })
</div>
</div>
So when the holeview posts back holeNumber, Par, Length, StrokeIndex, CourseID. it passes all data into the hole table other than the courseId which it posts a null.
What am I missing from the POST to get the CourseId to be entered in the Database.
In my views, I have a few drop-down lists that I use to get the ID and show the value to the user so they know what they are choosing. But in the view that I use to list everything that has already been added, it shows the chosen ID, but I want the name to appear
I tried this but it always shows the status that has the ID 1, which is the status "Active"
#Html.DropDownListFor(modelItem => item.status,
(SelectList)ViewBag.listStatus,
new { #class disabled = "" })
//controller
{
public ActionResult listStatus()
{
var list= ListsDAO.listStatus();
return View(list);
}
}
//DAO
{
public static IEnumerable<LISTS> listStatus()
{
using (var ctx = new PadraoEntities())
{
var result= from lists in ctx.LISTS
where lists.TYPE_LIST== "STATUS"
select new
{
lists.IDE_LIST,
lists.DES_LIST,
lists.VLR_LIST
};
var list= new List<LISTS>();
foreach (var item in result)
{
list.Add(new LISTS()
{
IDE_LIST = item.IDE_LIST,
VLR_LIST = item.VLR_LIST,
DES_LIST = item.DES_LIST
});
}
return list;
}
}
}
//view add
<div class="form-group">
#Html.LabelFor(model => model.status, htmlAttributes: new { #class = "control -Label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.status(SelectList)ViewBag.listStatus, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.status, "", new { #class = "text-danger" })
</div>
</div>
//view list
{
#Html.DropDownListFor(modelItem => item.ststus,(SelectList)ViewBag.listStatus,new { disabled = "" })
}
when listing id 1, I expected "active" to appear, id 2 shows "inactive", etc
Try this //controller
public ActionResult listStatus()
{
ViewBag.Status = new SelectList(//Code to get id and its property, "StatusId", "Status");
var list= ListsDAO.listStatus();
return View(list);
}
//View
<div class="form-group">
#Html.LabelFor(model => model.Status, "TaskId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Status", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Status, "", new { #class = "text-danger" })
</div>
</div>
Problem is occurring here:
#Html.DropDownList("KategoriId", null, htmlAttributes: new { #class = "form-control" })
Here is my Controller:
public ActionResult Create()
{
ViewBag.KategoriId = new SelectList(db.Kategoris, "KategoriId", "KategoriAdi");
return View();
}
Here is my View:
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.KategoriId, "KategoriId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("KategoriId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.KategoriId, "", new { #class = "text-danger" })
</div>
</div>
You can do this Type Casting ViewBag to IEnumerable<SelectListItem> in CSHTML:
CSHTML Code:
#Html.DropDownList("your_key", (IEnumerable<SelectListItem>)ViewBag.KategoriId, "--Select Any--", new { #class = "form-control"})
Controller code:
IList<SelectListItem> items = new List<SelectListItem>();
foreach (var item in db.Kategoris.ToList())
{
SelectListItem sellist = new SelectListItem();
sellist.Value = item.code.ToString();
sellist.Text = item.name;
items.Add(sellist);
}
ViewBag.KategoriId = items; // at the end this item should be list of `SelectListItem`
Write your #Html.DropDownList() as follows:
#Html.DropDownList("KategoriId", ViewBag.KategoriId as SelectList,"Select Ketegory Id", htmlAttributes: new { #class = "form-control" })
This should fix your problem!
Im new to ASP .NET MVC Web Aplications and i please need some help. I need to summ two values ("Cena" and "Kolicina") and save into textbox "Znesek". How should i do this? I tried this (How to multiply values of two textboxes in Asp.net MVC) but its not working for me :/
Here is my .cshtml file:
<div class="form-group">
#Html.LabelFor(model => model.kolicina, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.kolicina, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.kolicina, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.cena, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.cena, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.cena, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.znesek, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.znesek, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.znesek, "", new { #class = "text-danger" })
</div>
</div>
Please help me, what i need to change or where i need to do some action?
EDIT:
Sure, i have #Html.BeginForm and everything what is needed for View code, this is just partial code. Everything is working, i can save all to SQL database and display it, i just down know how to multiple fields...
Here is my controler POST Code:
public ActionResult Create()
{
ViewBag.zapStDoumenta_tk = new SelectList(db.dokumentGlava, "zapStDokumenta", "zapStDokumenta");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "zapStPostavke,artikel,kolicina,cena,znesek,davek,popustNaPostavko,zapStDoumenta_tk")] postavkaDokumenta postavkaDokumenta)
{
if (ModelState.IsValid)
{
db.postavkaDokumenta.Add(postavkaDokumenta);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.zapStDoumenta_tk = new SelectList(db.dokumentGlava, "zapStDokumenta", "krajIzdaje", postavkaDokumenta.zapStDoumenta_tk);
return View(postavkaDokumenta);
}
Try this javascript code. This should work.
$(function(){
$("#kolicina,#cena").keyup(function(e){
var val1=$("#kolicina").val(),
val2=$("#cena").val(),
result="";
if(val1.length > 0){
result += val1;
}
if(val2.length > 0){
result += val2;
}
$("#znesek").val(result);
});
});
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,FromTime,ToTime,CustomerID,UserID,Description,Type,CreatedTime")] Schedule schedule)
{
if (ModelState.IsValid)
{
db.Entry(schedule).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CustomerID = new SelectList(db.Customers, "ID", "Name", schedule.CustomerID);
ViewBag.UserID = new SelectList(db.Users, "ID", "Name", schedule.UserID);
//return View(schedule);
return RedirectToRoute("Default", new { controller = "Schedules", action = "Index" });
}
<div class="form-group">
#Html.LabelFor(model => model.FromTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FromTime, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FromTime, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ToTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ToTime, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ToTime, "", new { #class = "text-danger" })
</div>
</div>
The type of FromTime and ToTime is long and i want to display it as Time and then save it back as long.
If I have a type of long in the database and i need to display it as Time in Model View Controller ASP.Net then save it as a long again into the database. How can i do this?