How to bind a list using MVC .net? [duplicate] - asp.net-mvc

This question already has answers here:
Post an HTML Table to ADO.NET DataTable
(2 answers)
Closed 6 years ago.
I have class OrderSummaryViewModel
public class OrderSummaryViewModel
{
public int Id { get; set; }
public string PurchaseOrderNumber { get; set; }
public string DeliveryCondition { get; set; }
/// <summary>
/// Order items
/// </summary>
public List<OrderItem> Items { get; set; }
}
And second class OrderItem
/// <summary>
/// Order items
/// </summary>
public class OrderItem
{
public int Id { get; set; }
public string ItemNumber { get; set; }
public string DeliveredQuantity { get; set; }
public string fullyDelivered { get; set; }
}
And i bind model OrderSummaryViewModel into below .csHTML view to show the order with its related items.
#model OrderSummaryViewModel
<div class="baseForm">
<label>PO Number:</label></td>
#Html.DisplayFor(model => model.PurchaseOrderNumber)
<label>Delivery Condition:</label>
#Html.DisplayFor(model => model.DeliveryCondition)
#using (Ajax.BeginForm("RegisterGoodsReceipt",FormMethod.Post, new AjaxOptions { OnSuccess = "OnSuccessRegisterGoodsReceipt", OnFailure = "OnFailureRegisterGoodsReceipt" }, null))
{
var items= Model.Items;
<table class="dataGrid">
#for (var i = 0; i < items.Count; i++)
{
var item = items[i];
<tr>
<td>#Html.DisplayFor(modelItem => item.Id)</td>
<td>#Html.DisplayFor(modelItem => item.ItemNumber)</td>
<td>
<input class="fullyDelivered" id="#(item.Id)" name="fullyDelivered" type="checkbox" />
<input type="text" id="deliveredQuantity#(item.Id)" name="DeliveredQuantity" value="#item.DeliveredQuantity"/>
</td>
</tr>
}
</table>
<button type="submit" class="button">Submit</button>
}
</div>
I want to submit the list of items to below action as below
public ActionResult RegisterGoodsReceipt(OrderSummaryViewModel order)
{
//to do
}
But it always go with null, i tried also to send it as list of items and it gives null also.
public ActionResult RegisterGoodsReceipt(List<OrderItem> items)
{
//to do
}
so any help regarding this issue?

By removing HTML code and writing it using Razor problem solved and it now sends the model with the right data

Related

Hello guys,how can i post list with ajax to MVC

[HttpPost]
public ActionResult check(List<tbl_checklist> chk)
{
db.tbl_checklist.AddRange(chk);
db.SaveChanges();
return RedirectToAction("den_list", "denetim");
}
As you see,i want list data from view.But i can not
This is the approach I use, which uses Kendo but doesn't have to: https://onallthingsweb.wordpress.com/2016/06/27/asp-net-mvc-and-client-side-lists-with-kendo/
Essentially, using this model as an example:
public class SystemTestModel
{
public List Entities { get; set; }
}
public class SystemListEntity
{
public string Name { get; set; }
public string Value { get; set; }
public bool IsDeleted { get; set; }
}
You can bind to the Entities property like this:
for (var i = 0; i < Model.Entities.Count; i++)
{
<tr>
<td>
<input type="hidden" name="#Html.NameFor(m => m.Entities[i].IsDeleted)" value="#Boolean.FalseString" />
</td>
<td>
<input type="text" value="#= Value #" name="#Html.NameFor(m => m.Entities[i].Value)" />
</td>
</tr>
}
When this form is posted back to the server, your action method will receive all the data as necessary:
public ActionResult Index(SystemTextModel model)
{
foreach (var entity in model.Entities)
{
//the data in the UI is passed back to the server
}
}

Dropdown list population from ViewModel

First of all, I know this question has been asked many, many times. I've read countless articles and Stack Overflow answers. I've tried to figure this problem out for four days and I think I need help if someone doesn't mind.
I have two databases. The employee database has a field called "DisplayName" -- the second database has a relationship with the first and they work together great. I'm able to call the two databases perfectly in another application.
You can see the in the picture Index Page
that I have a list of people. I want a dropdown below it that lists all display names in the database so employees can add themselves to the list. You'll see a dropdown in the image but it's not populated.
Seems simple. But geez. Part of a problem I'm having is my home controller already has a function to populate the list in the picture so I can't do another on that page. I've tried a lot of suggestions on a lot of sites. I get IEnumerable errors or display reference errors....
Here's my controller (again - it has nothing in it that helps the dropdown):
namespace SeatingChart.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Employee
public ActionResult Index()
{
var lists = db.BreakModels
.Include("Employee")
.Include("TimeEntered")
.Include("TimeCleared")
.Include("DisplayName")
.Select(a => new HomeIndexViewModels
{
Employee = a.Employee,
DisplayName = a.EmployeeModels.DisplayName,
TimeEntered = a.TimeEntered,
TimeCleared = a.TimeCleared.Value,
Id = a.EmployeeModels.Id,
});
return View(lists);
}
View:
#model IEnumerable<SeatingChart.Models.HomeIndexViewModels>
#{
Layout = null;
}
#Html.Partial("_Header")
<div class="container_lists">
<div class="container_break col-md-8">
<h5 style="text-align:center">Break List</h5>
<table class="table-bordered col-lg-12">
#if (Model != null)
{
foreach (var item in Model)
{
if (item.TimeCleared == null)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
 BV
</td>
<td>
 #item.TimeEntered.ToString("HH:mm")
</td>
</tr>
}
}
}
</table>
#using (Html.BeginForm())
{
<div class="row site-spaced">
<div class="col-3">
#Html.DropDownList("DisplayName", new SelectList(new List<string>() { "---Dispatcher---" }), new { #class = "required " })
</div>
</div>
<div class="col-3">
<input type="submit" value="Submit" class="site-control" />
</div>
}
</div>
</div>
ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace SeatingChart.Models
{
public class HomeIndexViewModels
{
//Break Model
public int BreakId { get; set; }
public int Employee { get; set; }
public DateTime TimeEntered { get; set; }
public DateTime? TimeCleared { get; set; }
//Employee Model
public int Id { get; set; }
public string DisplayName { get; set; }
public string DisplayNames { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool NotActive { get; set; }
public int Force { get; set; }
public string EmployeeList { get; set; }
}
}
I hope this is clear enough. I've tried so many different ways with so much code - the errors are different with everything I've tried.
Thanks in advance for your patience and help!
You can add to your viewmodel
public List<SelectListItem> Employees { get; set; }
Then you can populate this list with controller then in view just call it with:
#Html.DropDownListFor(m => m.Id, Model.Employees, new { #class = "form-control", required = "required" })
Update - how to populate list. Should work (but not tested code).
public List<SelectListItem> GetEmployeeForDropdown(List<HomeIndexViewModels> list)
{
List<SelectListItem> empList = new List<SelectListItem>();
try
{
if (list != null && list.Count > 0)
{
foreach (var item in list)
{
empList.Add(new SelectListItem { Text = item.DisplayName, Value = item.Id.ToString() });
}
}
else
{
empList.Add(new SelectListItem { Text = "No items", Value = string.Empty });
}
}
catch (Exception ex)
{
//handle exceptions here
}
return empList;
}
Edit: Remember to use your model in view!

ASP.NET MVC 5 Html.CheckboxFor only return default value on post

I have read the tutorials and prepared a list of checkboxes for the page. When the form is submitted, the Selected property only get the value false.
Is there something I missed?
The Model
public class SelectStudentModel
{
public int StudentID { get; set; }
public string CardID { get; set; }
public string Name { get; set; }
public bool Selected { get; set;}
}
The ViewModel
public class SelectStudentViewModel
{
public List<SelectStudentModel> VMList;
public SelectStudentViewModel()
{
VMList = SelectStudentModel.GETStudent();
}
}
The View
#using Student.Models
#model SelectStudentViewModel
#using (Html.BeginForm("AddStudent", "SectionStudent", FormMethod.Post, new { #role = "form" }))
{
#{ for (int i = 0; i < Model.VMList.Count(); i++)
{
<tr>
<td>#Html.CheckBoxFor(m => m.VMList[i].Selected)</td>
<td>#Html.DisplayFor(model => model.VMList[i].Name)</td>
</tr>
}
}
<input type="submit" value="submit" />
}#* end form *#
The Controller for posted data
[HttpPost]
public ActionResult AddStudent(SelectStudentViewModel model)
{
foreach (SelectStudentModel m in model.VMList)
{
Console.Write(m.Selected.ToString());
}
return PartialView("StudentSelectForm", model);
}
VMList is a field in your SelectStudentViewModel model. You need to change it to a property (with a getter/setter) so the DefaultModelBinder can set the values
public class SelectStudentViewModel
{
public List<SelectStudentModel> VMList { get; set; } // change
public SelectStudentViewModel()
{
VMList = SelectStudentModel.GETStudent();
}
}
Side note: Suggest you change #Html.DisplayFor(model => model.VMList[i].Name) to #Html.LabelFor(m => m.VMList[i].Selected, Model.MList[i].Name) so that you get a label associated with the checkbox

kendo multiselect, send selected values to controller

I'm new to ASP.NET mvc and kendo ui framework and have the following issue:
I have a partialview in a window with a multiselect which receives its values out of the database. the view looks like:
#model SoftwareAdminInterface.Models.Administration.Pattern
<div id="myContentPopupEditRole_div">
#using (Ajax.BeginForm("SetCombi", "Pattern", new { }, new AjaxOptions() { HttpMethod = "post", UpdateTargetId = "myContentPopupEditRole_div" }))
{
<center>
<br />
<table class="table_no_borders">
<tr>
<td style="width: 300px">
#(
Html.Kendo().MultiSelectFor(model => model.RegExId)
.MaxSelectedItems(2)
.Name("RegExID")
.DataTextField("RegExName")
.DataValueField("RegExID")
.Placeholder("Select Patterns...")
.AutoBind(false)
.DataSource(source => {
source.Read(read =>
{
read.Action("GetPatternsForCombi", "Pattern");
})
.ServerFiltering(true);
})
)
</td>
</tr>
</table>
<button class="k-button k-button-icontext k-grid-custom" id="get" type="submit">#Resources.General.BtnSave</button>
</center>
}
</div>
I'm using a model which looks like this:
public class Pattern
{
[ScaffoldColumn(false)]
public int RegExID { get; set; }
[Display(Name = "RegEx")]
[Required]
public string RegExName { get; set; }
[UIHint("GridForeignKey")]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string ColumnName { get; set; }
public string RegExTable { get; set; }
[UIHint("GridForeignKey")]
public int? TableID { get; set; }
public string Version { get; set; }
public string Description { get; set;
}
in the SetCombi function which is called in the patterncontroller, I only want to receive the two ID's of the selected objects out of the multiselect, but I've no idea how they should be send to the controller.
thx in advance for your help
I think you have two options:
1) Change RegExID to be a string and then I believe they would come across as comma separated values.
2) (The better option) Add a ViewModel which has many of the same properties as your Pattern model above. The main difference for this is that you would have
public List<int> RegExIDs { get; set; }
Then in your controller you would take the new ViewModel as an argument to your post method and parse out the values in the list as necessary.

How do I bind checkboxes to the List<int> property of a view model?

I've been reading the various posts on view models and check boxes, but my brain is starting to lock up and I need a little push in the right direction.
Here's my simplified view model. I have checkboxes that need to populate the lists with their values. I don't think this can happen automagically. I'm not sure how to bridge the gap between an array of string values and a List correctly. Suggestions?
public int AlertId { get; set; }
public List<int> UserChannelIds { get; set; }
public List<int> SharedChannelIds { get; set; }
public List<int> SelectedDays { get; set; }
Have your View Model like this to represent the CheckBox item
public class ChannelViewModel
{
public string Name { set;get;}
public int Id { set;get;}
public bool IsSelected { set;get;}
}
Now your main ViewModel will be like this
public class AlertViewModel
{
public int AlertId { get; set; }
public List<ChannelViewModel> UserChannelIds { get; set; }
//Other Properties also her
public AlertViewModel()
{
UserChannelIds=new List<ChannelViewModel>();
}
}
Now in your GET Action, you will fill the values of the ViewModel and sent it to the view.
public ActionResult AddAlert()
{
var vm = new ChannelViewModel();
//The below code is hardcoded for demo. you mat replace with DB data.
vm.UserChannelIds.Add(new ChannelViewModel{ Name = "Test1" , Id=1});
vm.UserChannelIds.Add(new ChannelViewModel{ Name = "Test2", Id=2 });
return View(vm);
}
Now Let's create an EditorTemplate. Go to Views/YourControllerName and Crete a Folder called "EditorTemplates" and Create a new View there with the same name as of the Property Name(ChannelViewModel.cshtml)
Add this code ro your new editor template.
#model ChannelViewModel
<p>
<b>#Model.Name</b> :
#Html.CheckBoxFor(x => x.IsSelected) <br />
#Html.HiddenFor(x=>x.Id)
</p>
Now in your Main View, Call your Editor template using the EditorFor Html Helper method.
#model AlertViewModel
<h2>AddTag</h2>
#using (Html.BeginForm())
{
<div>
#Html.LabelFor(m => m.AlertId)
#Html.TextBoxFor(m => m.AlertId)
</div>
<div>
#Html.EditorFor(m=>m.UserChannelIds)
</div>
<input type="submit" value="Submit" />
}
Now when You Post the Form, Your Model will have the UserChannelIds Collection where the Selected Checkboxes will be having a True value for the IsSelected Property.
[HttpPost]
public ActionResult AddAlert(AlertViewModel model)
{
if(ModelState.IsValid)
{
//Check for model.UserChannelIds collection and Each items
// IsSelected property value.
//Save and Redirect(PRG pattern)
}
return View(model);
}
Part of My View Model:
public List<int> UserChannelIds { get; set; }
public List<int> SharedChannelIds { get; set; }
public List<int> Weekdays { get; set; }
public MyViewModel()
{
UserChannelIds = new List<int>();
SharedChannelIds = new List<int>();
Weekdays = new List<int>();
}
I used partial views to display my reusable checkboxes (I didn't know about editor templates at this point):
#using AlertsProcessor
#using WngAlertingPortal.Code
#model List<int>
#{
var sChannels = new List<uv_SharedChannels>();
Utility.LoadSharedChannels(sChannels);
}
<p><strong>Shared Channels:</strong></p>
<ul class="channel-list">
#{
foreach (var c in sChannels)
{
string chk = (Model.Contains(c.SharedChannelId)) ? "checked=\"checked\"" : "";
<li><input type="checkbox" name="SharedChannelIds" value="#c.SharedChannelId" #chk /> #c.Description (#c.Channel)</li>
}
}
All three checkbox partial views are similar to each other. The values of the checkboxes are integers, so by lining up my view model List names with the checkbox names, the binding works.
Because I am working in int values, I don't feel like I need the extra class to represent the checkboxes. Only checked checkboxes get sent, so I don't need to verify they are checked; I just want the sent values. By initializing the List in the constructor, I should be avoiding null exceptions.
Is this better, worse or just as good as the other solution? Is the other solution (involving an extra class) best practice?
The following articles were helpful to me:
http://forums.asp.net/t/1779915.aspx/1?Checkbox+in+MVC3
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Binding list with view model
This site handles it very nicely
https://www.exceptionnotfound.net/simple-checkboxlist-in-asp-net-mvc/
public class AddMovieVM
{
[DisplayName("Title: ")]
public string Title { get; set; }
public List<CheckBoxListItem> Genres { get; set; }
public AddMovieVM()
{
Genres = new List<CheckBoxListItem>();
}
}
public class MembershipViewData
{
public MembershipViewData()
{
GroupedRoles = new List<GroupedRoles>();
RolesToPurchase = new List<uint>();
}
public IList<GroupedRoles> GroupedRoles { get; set; }
public IList<uint> RolesToPurchase { get; set; }
}
//view
#model VCNRS.Web.MVC.Models.MembershipViewData
#{
ViewBag.Title = "MembershipViewData";
Layout = "~/Views/Shared/_Layout.cshtml";
int i = 0;
}
#using (Html.BeginForm("Membership", "Account", FormMethod.Post, new { id = "membershipForm" }))
{
<div class="dyndata" style="clear: left;">
<table width="100%" cellpadding="0" cellspacing="0" class="table-view list-view">
foreach (var kvp2 in Model.GroupedRoles)
{
string checkBoxId = "RolesToPurchase" + kvp2.RoleType;
<tr>
<td width="240px">
<label class="checkbox-label" for="#checkBoxId">
<input type="checkbox" class="checkbox" name="RolesToPurchase[#i]"
id="#checkBoxId" value="#kvp2.RoleType" />
#kvp2.Key
</label>
</td>
</tr>
i++;
}
<tr style="background-color: #ededed; height: 15px;">
<td colspan="5" style="text-align: right; vertical-align: bottom;">
#Html.SubmitButton(Resources.MyStrings.Views_Account_Next)
</td>
</tr>
</table>
</div>
}
//Post Action
[HttpPost]
public ActionResult Membership(MembershipViewData viewData)
{
..........................
}
}

Resources