Model Property not updated after dropdownlist selection mvc3 - asp.net-mvc-3

#{
var items = new List<SelectListItem>(){
new SelectListItem {Text = "One", Value = "1"},
new SelectListItem {Text = "Two", Value = "2"},
new SelectListItem {Text = "Three", Value = "3"},
new SelectListItem {Text = "Four", Value = "4"},
new SelectListItem {Text = "Five", Value = "5"}
};
}
#Html.DropDownListFor(x => x.InvoiceItem.Count, new SelectList(items, "Value", "Text", Model.InvoiceItem.Count), new { style = "width:95%" })
Model.InvoiceItem.Count is integer and is set to 5 in the controller.
When the view is loaded, I see 'Five' as the default value in the drop down list, but when I post back to the controller, the model property Invoice.Count is always '0'. Can any one suggest what I am missing. Thanks in advance.
Here is my controller with add and save action methods
public ActionResult Add() {
if (PassportSession.GetPassportProfile() == null)
return PartialView("~/Areas/Ship/Views/Ship/_Redirect.cshtml");
var invoiceModel = (InvoiceModel)Session["InvoiceModel"];
invoiceModel.InvoiceItem = new InvoiceItem();
invoiceModel.InvoiceItem.OriginCountry = "US";
invoiceModel.InvoiceItem.Count = 5;
invoiceModel.AddEdit = true;
invoiceModel.Add = true;
invoiceModel.InvoiceItem.Line = invoiceModel.InvoiceItems.Count + 1;
invoiceModel.InvoiceItems.Add(invoiceModel.InvoiceItem);
invoiceModel.InvoiceItem.Unit = "EA ";
Session["InvoiceModel"] = invoiceModel;
return PartialView("_Footer", invoiceModel);
}
public ActionResult Save(InvoiceModel returnModel) {
if (PassportSession.GetPassportProfile() == null)
return PartialView("~/Areas/Ship/Views/Ship/_Redirect.cshtml");
//if (string.IsNullOrEmpty(returnModel.InvoiceItem.PartNumber))
// ModelState.AddModelError("InvoiceItem.PartNumber", "Part Number is required.");
if (string.IsNullOrEmpty(returnModel.InvoiceItem.OriginCountry))
ModelState.AddModelError("InvoiceItem.OriginCountry", "Origin Country is Required.");
if (string.IsNullOrEmpty(returnModel.InvoiceItem.Description))
ModelState.AddModelError("InvoiceItem.Description", "Part Description is Required.");
if (!string.IsNullOrEmpty(returnModel.InvoiceItem.HarmCode) && ((returnModel.InvoiceItem.HarmCode.Length < 6) || (returnModel.InvoiceItem.HarmCode.Length > 15)))
ModelState.AddModelError("InvoiceItem.HarmCode", "Tariff is 6-15 chars.");
if (returnModel.InvoiceItem.Price<=0)
ModelState.AddModelError("InvoiceItem.Price", "Price need to be greater than Zero.");
var invoiceModel = (InvoiceModel)Session["InvoiceModel"];
if (ModelState.IsValid) {
invoiceModel.InvoiceItem = new InvoiceItem();
invoiceModel.AddEdit = false;
var invoiceItem = invoiceModel.InvoiceItems.Where(x => x.Line == returnModel.InvoiceItem.Line).FirstOrDefault();
if (invoiceItem == null && returnModel.InvoiceItem.Line>0) //this is for Covance Change Request
{
invoiceItem = new InvoiceItem();
invoiceModel.InvoiceItems.Add(invoiceItem);
}
invoiceItem.Count = returnModel.InvoiceItem.Count;
invoiceItem.CreateDate = DateTime.Now;
invoiceItem.Custom01 = returnModel.InvoiceItem.Custom01;
invoiceItem.Custom02 = returnModel.InvoiceItem.Custom02;
invoiceItem.Custom03 = returnModel.InvoiceItem.Custom03;
invoiceItem.Custom04 = returnModel.InvoiceItem.Custom04;
invoiceItem.Custom05 = returnModel.InvoiceItem.Custom05;
invoiceItem.Custom06 = returnModel.InvoiceItem.Custom06;
invoiceItem.Custom07 = returnModel.InvoiceItem.Custom07;
invoiceItem.Custom08 = returnModel.InvoiceItem.Custom08;
invoiceItem.Custom09 = returnModel.InvoiceItem.Custom09;
invoiceItem.Custom10 = returnModel.InvoiceItem.Custom10;
invoiceItem.Description = returnModel.InvoiceItem.Description;
invoiceItem.HarmCode = returnModel.InvoiceItem.HarmCode;
invoiceItem.Line = returnModel.InvoiceItem.Line;
invoiceItem.OriginCountry = returnModel.InvoiceItem.OriginCountry;
invoiceItem.PartNumber = string.IsNullOrEmpty(returnModel.InvoiceItem.PartNumber) ? string.Empty : returnModel.InvoiceItem.PartNumber;
invoiceItem.Price = returnModel.InvoiceItem.Price;
//invoiceItem.ShipId = returnModel.InvoiceItem.ShipId;
invoiceItem.Unit = returnModel.InvoiceItem.Unit;
Session["InvoiceModel"] = invoiceModel;
}
return PartialView("_Footer", invoiceModel);
}
In the add action method I am setting the count property to 5 and pass it to the view, but when I post the form back to save method it is 0.

Related

Bind List to GridviewComboboxcolumn in telerik radgrindview (winform)

I have a generic List like
List<return> returnlist
class return
{
public string returnid {get; set;
...
public List<string> Vouchernumbers
}
I bind the returnlist to the telerik radgridview.
How can i bind the voucherlist to the GridviewComboboxcolumn for each row ?
I have bind the voucherlist to the combobox after radgridview_complete_binding.
You need to create the grid with columns and data
You need to add the combobox column, initialize it.. Please check you need to have a dataeditor in here
Assign the string to datasource
comboColumn.DataSource = new String[] { "Test1", "Test2"};
You can bind the collections too:
Binding list BindingList<ComboBoxDataSourceObject> list = new BindingList<ComboBoxDataSourceObject>();
ComboBoxDataSourceObject object1 = new ComboBoxDataSourceObject();
object1.Id = 1;
object1.MyString = "Test 1";
list.Add(object1);
ComboBoxDataSourceObject object2 = new ComboBoxDataSourceObject();
object2.Id = 2;
object2.MyString = "Test 2";
list.Add(object2);
colboCol2.DataSource = list;
radGridView1.Columns.Add(colboCol2);
create radcombobox and set datasource and add it to rad grid
eg :
GridViewComboBoxColumn col = new GridViewComboBoxColumn();
col.DataSource = DAL.ActiveDb.GetList<SalesRep>().ToList().OrderBy(x => x.RepName).Select(x => new { Id = x.Id, x.RepName });
col.DropDownStyle = RadDropDownStyle.DropDown;
col.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
col.DisplayMember = "RepName";
col.ValueMember = "Id";
col.FieldName = "RepId";
col.HeaderText = "Rep Name";
col.Width = 200;
//var t = gridColInfo.Where(x => x.ColumnName.ToLower() == "repid").FirstOrDefault();
//if (t != null)
//{
// col.Width = t.ColumnWidth;
//}
this.radGridBillwiseOpening.Columns.Add(col);

How can I set a default value to the SelectListItem constructor?

How can I set a default value to the SelectListItem constructor? I've been trying to do:
var categories = new List<SelectListItem>();
for (int i = 0; i < rubricsList.Count(); i++)
{
categories.Add(new SelectListItem
{
Text = rubricsList[i],
Value = rubricsList[i],
Selected = rubricsList[3],ToString().Equals(true)
});
}
and...
Selected = rubricsList[3]).ToString().Equals(rubricsList[3])
EDIT:
Here's where I'm using the list:
#Html.DropDownListFor(x=>x.Category, Model.Categories,"Select One")
...not working. I want to take a value from the list and make it a default value.
categories.Add(new SelectListItem
{
Text = rubricsList[i],
Value = rubricsList[i],
Selected =true
});
then in the view
#{string theSelectedOne=viewBag.selectedvalue;}
#Html.DropDownListFor(x=>x.Category, Model.Categories,theSelectedOne)
categories.Add(new SelectListItem() { Text=rubricsList[i], Value=rubricsList[i], Selected= true});
try this is so simple

Why does my selected value not get added to my SelectListItem as the “Text” and “Value” properties do?

I am trying to set a default value of a list in a partial view. The partial view is called using this…
#Html.Action("Acton", "Controller", new { department = item.Data.departmentNumber, defaultValue="someValue" })
Then in the controller I have…
[ChildActionOnly]
public ActionResult Categories(int? id, int? department, string defaultValue)
{
var typeList = from e in db.Rubrics where e.DepartmentID == department select e;
var selectedRubrics = typeList.Select(r => r.Category);
List<String> rubricsList = selectedRubrics.ToList();
var categories = new List<SelectListItem>();
for (int i = 0; i < rubricsList.Count(); i++)
{
categories.Add(new SelectListItem
{
Text = rubricsList[i],
Value = rubricsList[i],
Selected = (defaultValue == "defaultValueGetsSentToView")
});
}
var ViewModel = new RubricsViewModel
{
Category = "Select a Category",
Categories = categories
};
return View(ViewModel);
}
Why does my selected value not get added to my SelectListItem as the “Text” and “Value” properties are? Thanks for any help!
Assuming the values in the code are the literal values you are using, "defaultValue" is "someValue" and you are setting selected with the comparison defalutValue == "defaultValueGetsSentToView".
"someValue" == "defaultValueGetsSentToView" evaluates to false.

How to iterate through GroupBy groups using Dynamic LINQ? [duplicate]

I am using Dynamic Linq helper for grouping data. My code is as follows :
Employee[] empList = new Employee[6];
empList[0] = new Employee() { Name = "CA", State = "A", Department = "xyz" };
empList[1] = new Employee() { Name = "ZP", State = "B", Department = "xyz" };
empList[2] = new Employee() { Name = "AC", State = "B", Department = "xyz" };
empList[3] = new Employee() { Name = "AA", State = "A", Department = "xyz" };
empList[4] = new Employee() { Name = "A2", State = "A", Department = "pqr" };
empList[5] = new Employee() { Name = "BA", State = "B", Department = "pqr" };
var empqueryable = empList.AsQueryable();
var dynamiclinqquery = DynamicQueryable.GroupBy(empqueryable, "new (State, Department)", "it");
How can I get back the Key and corresponding list of grouped items i.e IEnumerable of {Key, List} from dynamiclinqquery ?
I solved the problem by defining a selector that projects the Key as well as Employees List.
var eq = empqueryable.GroupBy("new (State, Department)", "it").Select("new(it.Key as Key, it as Employees)");
var keyEmplist = (from dynamic dat in eq select dat).ToList();
foreach (var group in keyEmplist)
{
var key = group.Key;
var elist = group.Employees;
foreach (var emp in elist)
{
}
}
The GroupBy method should still return something that implements IEnumerable<IGrouping<TKey, TElement>>.
While you might not be able to actually cast it (I'm assuming it's dynamic), you can certainly still make calls on it, like so:
foreach (var group in dynamiclinqquery)
{
// Print out the key.
Console.WriteLine("Key: {0}", group.Key);
// Write the items.
foreach (var item in group)
{
Console.WriteLine("Item: {0}", item);
}
}

Create a store from Code Behind and filter its content and bind it to Combobox Ext.Net 2.2

I am creating combobox in a component Column in a GridPanel from codebehind. There is requirement to create a store and filter its content according to some condition and bind it to the created combobox. Store is getting bind to combo but filter is not working. Please help me to get the proper solution for this. My code snippet is given below.
List<object> storeDataProductClass= new List<object>();
storeDataProductClass.Add(new { text = "Class0", value = "Class0", productIndex = 0});
storeDataProductClass.Add(new { text = "Class1", value = "Class1", productIndex = 1});
storeDataProductClass.Add(new { text = "Class2", value = "Class2", productIndex = 2});
storeDataProductClass.Add(new { text = "Class3", value = "Class3", productIndex = 3});
storeDataProductClass.Add(new { text = "Class4", value = "Class4", productIndex = 4});
Ext.Net.ComboBox cmbClass = new ComboBox();
cmbClass.ID = "cmbClass_" + i;
Model classModel = new Model();
classModel.Fields.Add(new ModelField("text", ModelFieldType.String));
classModel.Fields.Add(new ModelField("value", ModelFieldType.String));
classModel.Fields.Add(new ModelField("productIndex", ModelFieldType.Int));
Ext.Net.Store storeClass = new Ext.Net.Store();
storeClass.ID = "storeClass_" + i;
storeClass.AutoDataBind = true;
storeClass.Model.Add(classModel);
storeClass.DataSource = storeDataProductClass;
storeClass.DataBind();
storeClass.Filter("productIndex", i.ToString());
cmbClass.Store.Add(storeClass);
cmbClass.DisplayField = "text";
cmbClass.ValueField = "value";
compColumn.Component.Add(cmbClass);

Resources