On my view I want to show a CheckBoxList that will show the names of all the students. Checking each student will ultimately mark their attendence. What I want to do is pass a ViewBag that (supposedly) contains a SelectList of students.
Here is what I am trying to do in Controller.
public ActionResult Create(int? id)
{
List<SelectListItem> selectListItems = new List<SelectListItem>();
var #class = db.Classes.Find(id);
var students = #class.Students.ToList();
foreach (Student student in students)
{
SelectListItem item = new SelectListItem()
{
Text = student.St_name,
Value = student.St_id.ToString(),
Selected = student.IsSelected
};
selectListItems.Add(item);
}
ViewBag.students = selectListItems;
var timetables = #class.Timetables.ToList();
ViewBag.Sat_ti_fk_id = new SelectList(timetables, "Ti_id", "Ti_day");
return View();
}
And this is what I am doing in my View
<div class="form-group">
#Html.LabelFor(model => model.Sat_st_fk_id, "Sat_st_fk_id", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.ListBoxFor(ViewBag.students, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.ValidationMessageFor(model => model.Sat_st_fk_id, "", new { #class = "text-danger" })
</div>
</div>
But the ListBoxFor method is showing error that says it takes 2 arguments.
Any sort of help is apreciated.
Thanks.
#Html.ListBoxFor Takes three arguments
The property that value are stored in
List of data to show as SelectList
Html attributes ( optional )
You should change the #Html.ListBoxFor to this
#Html.ListBoxFor(model => model.Sat_st_fk_id, ViewBag.students as SelectList, new { #class = "control-label col-md-2" })
Related
I asked a question yesterday about how to fix a sudden and unexpected "Object not set to an instance of an object" error. The only real answer I got was to pass a new instance of the object to the view like this:
// GET: WC_Inbox/Create
public ActionResult Create(int? id)
{
System.Diagnostics.Debug.WriteLine("Employee ID was: " + id);
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
string fullName = employee.First_Name + " " + employee.Last_Name;
System.Diagnostics.Debug.WriteLine("Employee full name: " + fullName);
ViewBag.EmployeeID = id;
ViewBag.Name = fullName;
ViewBag.Status = "Pending";
return View(new WC_Inbox());
}
With the primary line in question being here: return View(new WC_Inbox());
This worked, however it has created further issues. Now on my create page I am given some incorrect data in some fields
The fields Org Number, Hire Date, Work Schedule, and Injury date should not show those values. Those are incorrect and will be very confusing to the mostly elderly client bass the app is intended for. I would like for the placeholder values to show up there rather than this incorrect data.
This is the code from the Create view for those fields:
<div class="form-group">
#Html.LabelFor(model => model.Org_Number, "Org Number", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Org_Number, new { htmlAttributes = new { #class = "form-control", placeholder = "0025" } })
#Html.ValidationMessageFor(model => model.Org_Number, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Hire_Date, "Hire Date", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { #class = "form-control", placeholder = "8/3/2021" } })
#Html.ValidationMessageFor(model => model.Hire_Date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Job_Title, "Job Title", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Job_Title, new { htmlAttributes = new { #class = "form-control", placeholder = "Programmer Analyst" } })
#Html.ValidationMessageFor(model => model.Job_Title, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Work_Schedule, "Work Schedule", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Work_Schedule, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Work_Schedule, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Injury_Date, "Injury Date", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Injury_Date, new { htmlAttributes = new { #class = "form-control", placeholder = "8/14/2021" } })
#Html.ValidationMessageFor(model => model.Injury_Date, "", new { #class = "text-danger" })
</div>
</div>
How can I get rid of that bad data and use the placeholder values I assigned instead?
PS: Work Schedule should simply be blank. There is no placeholder value.
The issue is that you are passing a default object to the strongly-typed view. On the line:
return View(new WC_Inbox());
you have not done anything to initialize the properties/values on the WC_Inbox object. This means that all the values will be the default value (0 for int, null for string, the minimum date time for DateTime, etc.) It is those default values that you are seeing on your view. If you would like blank default values, you just have to update the WC_Inbox class to support nullable types. This works because null values display as blanks, so your placeholder text will show up. For example, it worked as expected for Job_Title since Job_Title is a string and its default value is null which is then displayed as a blank by EditorFor which allows the placeholder text to display. Hopefully that makes sense - it is simple, but verbose to explain.
If your WC_Inbox class can allow for nullable values, update it and then your placeholders would work. So the class now looks like:
class WC_Inbox
{
public int? Org_Number {get; set;}
public DateTime? Hire_Date { get; set;}
public string Job_Title {get; set;} // string is nullable already
public DateTime? Work_Schedule {get; set;}
public DateTime? Injury_Date {get; set;}
}
If you update the definition of WC_Inbox then the code as you have it will work.
I create a new form and I want view name based on id in another table, but the id can't view in the dropdown list:
This is the view:
<div class="form-group">
<label class="control-label col-md-2">ID Operator</label>
<div class="col-md-10">
#Html.DropDownList("idOp", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.idOp, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Name</label>
<div class="control-label col-md-10">
#Html.EditorFor(model => model.tbl_operator.nama, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.tbl_operator.nama , "", new { #class = "text-danger" })
</div>
</div>
This is the controller :
public ActionResult Create()
{
ViewBag.idEx = new SelectList(db.tbl_exercises, "idEx");
ViewBag.idOp = new SelectList(db.tbl_operator, "idOp");
return View();
}
Maybe I'm missing the code.
You need choose what field is Name, Value for SelectList like
ViewBag.idEx = new SelectList(tbl_exercises, "Value", "Name");
I made an example base on your code
var tbl_exercises = new List<exercises>
{
new exercises
{
Name = "Name 1",
Value = 10
},
new exercises
{
Name = "Name 2",
Value = 11
}
};
ViewBag.idEx = new SelectList(tbl_exercises, "Value", "Name");
SelectList is defined as
public SelectList(IEnumerable items, string dataValueField, string dataTextField);
In your controller, try to use
ViewBag.idOp = new SelectList(db.tbl_operator, "idOp","idOp");
I'm trying to populate a dropdown list with concatenated items to be used in a form . I have gotten the dropdown list to display those concatenated strings but I think it's failing to get an ID. (I'can't submit the form, dropdown blinks when I click submit), I take it this means no ID is being passed
Controller:
public ActionResult AdminCreate()
{
var query = (from e in db.Employees
select e.FullName + "-" + e.EmployeeID + "-" + e.Site.SiteName);
SelectList empList = new SelectList(query);
ViewBag.empList = empList;
return View();
}
View:
<div class="form-group">
#Html.LabelFor(model => model.EmployeeID, "Employee", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor((model => model.Employee.EmployeeID), (SelectList)ViewBag.empList, "--Select--", htmlAttributes: new { #class = "form-control"})
#Html.ValidationMessageFor(model => model.EmployeeID, "", new { #class = "text-danger" })
</div>
</div>
Make your query and SelecList as follows:
var employeeList = db.Employees.Select(e => new
{
e.EmployeeID
FullName = e.FullName + "-" + e.EmployeeID + "-" + e.Site.SiteName
}).ToList()
SelectList empList = new SelectList(employeeList,"EmployeeID","FullName");
In your #Html.DropDownListFor replace model.Employee.EmployeeID with model.EmployeeID as follows:
#Html.DropDownListFor(model => model.EmployeeID, (SelectList)ViewBag.empList, "--Select--", htmlAttributes: new { #class = "form-control"})
I'm trying to pass value from a view bag to the view create.
Controller
public ActionResult Create()
{
ViewBag.BanhoTosaId = new SelectList(db.BanhoTosas, "BanhoTosaId", "Tipo");
if (ViewBag.BanhoTosaId.SelectedValue != null)
{
BanhoTosa BT = db.BanhoTosas.Find(ViewBag.BanhoTosaId);
decimal valorSoma = BT.Valor + 10;
ViewBag.Total = valorSoma;
}
else
{
ViewBag.Total = 0;
}
return View();
}
view
<div class="form-group">
#Html.LabelFor(model => model.Total, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Total, new { htmlAttributes = new { #class = "form-control", value = "#ViewBag.Total" } })
#Html.ValidationMessageFor(model => model.Total, "", new { #class = "text-danger" })
</div>
</div>
In case BT will get the value in the BanhoTosa table and add 10, and then will return the value of the sum in the viewbag.Total.
But in view the #Html.EditorFor is receiving nothing
How can i do for the editorfor receive the value from the viewbag.total?
Thank's Guy's
I have 3 textboxes, 1st for Quantity, 2nd for Price and 3rd for Total Price.
<div class="form-group">
#Html.LabelFor(model => model.quantity, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.quantity, new { htmlAttributes = new { #class = "form-control", #type = "number" } })
#Html.ValidationMessageFor(model => model.quantity, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.price, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.price, new { htmlAttributes = new { #class = "form-control", #type = "number" } })
#Html.ValidationMessageFor(model => model.price, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.totalprice, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.totalprice, new { htmlAttributes = new { #class = "form-control", #type = "number" } })
#Html.ValidationMessageFor(model => model.totalprice, "", new { #class = "text-danger" })
</div>
and here is the controller:
[HttpPost]
public ActionResult Add(Model model)
{
obj.Quantity = model.quantity;
obj.Price = model.price;
obj.TotalPrice = model.totalprice
db.Details.Add(obj);
db.SaveChanges();
return RedirectToAction("");
}
now I want to multiply values of 1st and 2nd textboxes and show them in 3rd textbox. For example if users enter 5 in 1st textbox and 100 in 2nd textbox then it automatically shows 500 in 3rd textbox and if users changes value of 1st or 2nd textbox then value of 3rd textbox should also change accordingly.
Thanks.
You can listen to the keyup event of the textboxes in javascript, read the value and do the multiplication and set the resulting value to the third textbox.
Assuming your have jQuery library included in your page.
$(function(){
$("#quantity,#price").keyup(function(e){
var q=$("#quantity").val();
var p=$("#price").val();
var result="";
if(q!=="" && p!=="" && $.isNumeric(q) && $.isNumeric(p))
{
result = parseFloat(q)*parseFloat(p);
}
$("#totalPrice").val(result);
});
});
Here is a working jsbin sample.
You can use this:
[HttpPost]
public ActionResult Add(Model model)
{
obj.Quantity = model.quantity;
obj.Price = model.price;
obj.TotalPrice = model.totalprice
model.totalprice = model.quanity * model.price
db.Details.Add(obj);
db.SaveChanges();
return RedirectToAction("");
}
Hope it helps. I used it in my application and it does the right thing.