How to select multiple items from dropdown list, save it and again repopulate the dropdown with the selected list? - asp.net-mvc-3

I am working on an ASP.NET MVC3 project. I am facing issues regarding multiple selection in dropdown.
The problem is, I have to save multiple items in database from a dropdown list and repopulate it.
I have used the class below to represent each list data:
public class IDNameValueTO {
public int ID { get; set; } //Value of the selection Element
public string Name { get; set; } //Name of the selectionElement
public int Value { get; set; } //1 if value is checked and 0 if not
}
My List goes as follows:
public List<IDNameValueTO> tempList = new List<IDNameValueTO>();
ViewBag.SelectedList = tempList;
I am generating the dropdown list as follows:
#Html.DropDownList("SelectedValue", new SelectList(ViewBag.SelectedList, "ID", "Name"))
Now how do I save the multiple selection and display it later on using dropdown?

The default DropDown helper that is provide in asp.net mvc does not support multiselet.
You'll have to create your own custom multiselect dropdown helper.
Here are some links that can help you create you own custom dropdown helper:
http://blogs.msdn.com/b/rickandy/archive/2012/01/30/asp-net-mvc-dropdownlist-multiselect-and-jquery.aspx
http://utsavized.com/chosen-multiselect-dropdown-list-with-asp-net-mvc3/

Related

How to return result to a partial view with kendo grid and kendo textboxes

I'm having a problem in returning values to my view for kendo grid & fields.
Earlier I had only kendo grid in my partial view and hence I used below code to return the values of my grid:
public virtual ActionResult GetValues(long Id1, [DataSourceRequest]DataSourceRequest request)
{
return Json(ViewModel.List<Another_View_Model>.ToDataSourceResult(request));
}
My View Model structure is as follows
ViewModel
{
public long Id { get; set; }
public List<Another_View_Model> Another_View_Model { get; set; }
}
But now, I'm adding kendo textboxes, checkboxes to the same partial view and would like to return server values to those fields too while returning grid values.
My View Model structure is as follows
ViewModel
{
public long Id { get; set; }
public List<Another_View_Model> Another_View_Model { get; set; }
public string textboxField { get; set; }
}
In my controller, I'm doing the following changes but my textbox field values are not returning to the view.
public virtual PartialViewResult GetValues(long Id1)
{
return PartialView("_PartialView", ViewModel);
}
Can anyone please point me where I'm doing wrong or is there a better wayto return result for both grid & kendo elements at the same time within the same model.
My view structure is as follows:
#model ViewModel
#(Html.Kendo().TextBoxFor(p => p.textboxField)
.Name("TextBox")
)
#(Html.Kendo().Grid<Another_View_Model>()
.Name("KendoGrid")
Any help with this is appreciated. Thanks in advance!!
Use either TextBoxFor(p => p.PropertyName) or TextBox().Name("PropertyName") DO NOT use both. The name property will override the TextBoxFor. So in your example, your Kendo Textbox is actually binding to a Property named TextBox instead of textboxField like you were expecting.
The inverse is also true, if you were posting a form, the Model's textboxField will be null, while if you had a string parameter named TextBox it will be populated with the textbox's value

Kendo Combobox - reset value when the selected value is not in the datasource

We were using Telerik MVC extensions for ASP.NET MVC and have started migrating to Kendo UI now.
I am facing an issue with Kendo Combo and the details are as follows:
Suppose that I am binding a Kendo combo with a list of ten items (whose values are from 1 to 10 along with a blank item (say "Select" and it's value is "")) and in my markup, if I say .Value("0") - I don't want the kendo combo to show me "0" but instead get reset to the blank item or may be the first item in the list.
How can I achieve this ?
Regards
Your int has to be a nullable type.
ex:
public class SomeDTO
{
public int? ID {get; set;} //must be type int? and not int
public string Name {get; set;}
}
// on your kendo binding, .DataValueField('ID')

Set first element in select list as guid empty

I have a dropdownlist as below:-
#Html.DropDownListFor(model => model.NewPipelineEaPersonId, new SelectList(Model.EaList, "PersonId", "FullName"), "Please select one", new { style = "width: 250px;" })
I have my class as follows:-
public class LeadPipelineEntry
{
[Required]
public int PipelineTransactionId { get; set; }
public Guid NewPipelineEaPersonId { get; set; }
}
The Guid is not required in this part but still it is throwing a required field error.
Also I dont want to make guid as nullable parameter
I know that select field do these.
But can I make the value of 1st Item "Please select one" as Guid.Empty?
I want to manage this in the view only.
No, you cannot set the default element of a dropdown list to anything else than an empty string. This simply isn't supported by the DropDownListFor helper. At that's how it should be. You should make the Guid nullable on your view model if you want to allow empty values inside the dropdown.
If you cannot make the Guid nullable on the view model it makes absolutely no sense to provide a default option ("Please select one") for the dropdown list.
You could write a custom DropDownList helper which will provide some default value other than an empty string or generate the select manually - both equally wrong approaches.

Binding DropdownList and maintain after postback

I am using MVC3. I'm binding the dropdown with the Data coming from a service. But after the page posts back and a filter applies to list, the dropdown shows the filter record value in the grid because I always bind the list coming from the service.
However, I want the dropdown to always show all the Records in the database.
I don't understand your question that clearly. But it seems that it is a dropdown that you have on your view? I also have no idea what you are trying to bind so I created my own, but have a look at my code and modify it to fit in with your scenario.
In your view:
#model YourProject.ViewModels.YourViewModel
On the view there is a list of banks in a dropdown list.
Your banks dropdown:
<td><b>Bank:</b></td>
<td>
#Html.DropDownListFor(
x => x.BankId,
new SelectList(Model.Banks, "Id", "Name", Model.BankId),
"-- Select --"
)
#Html.ValidationMessageFor(x => x.BankId)
</td>
Your view model that will be returned to the view:
public class YourViewModel
{
// Partial class
public int BankId { get; set; }
public IEnumerable<Bank> Banks { get; set; }
}
Your create action method:
public ActionResult Create()
{
YourViewModel viewModel = new YourViewModel
{
// Get all the banks from the database
Banks = bankService.FindAll().Where(x => x.IsActive)
}
// Return the view model to the view
// Always use a view model for your data
return View(viewModel);
}
[HttpPost]
public ActionResult Create(YourViewModel viewModel)
{
if (!ModelState.IsValid)
{
// If there is an error, rebind the dropdown.
// The item that was selected will still be there.
viewModel.Banks = bankService.FindAll().Where(x => x.IsActive);
return View(viewModel);
}
// If you browse the values of viewModel you will see that BankId will have the
// value (unique identifier of bank) already set. Now that you have this value
// you can do with it whatever you like.
}
Your bank class:
public class Bank
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
This is simple as it gets. I hope this helps :)
PS: Please remember with future posts, always give as much detail as possible so that we can help you better. Also don't forget to display code samples so that we can see what you have already done. The more details that we can have the better.
When you post the model back to the create[Http Post] action is it not possible to have the list of dropdown list values for the banks binded back to the model. I see that if the model is invalid, you call the code
viewModel.Banks = bankService.FindAll().Where(x => x.IsActive);
to get a list of all the banks again which I assume you need to hit the database again.
Thanks

Listboxfor MVC3 Razor. User selection from database fields

I'm working with a Razor MVC3 project
I have a model
public class ProductFormatModel
{
public int ID {get; set;}
public string SerialID { get; set; }
public ProductTypes ProductType { get; set; }
public List<ProductComponent> Components { get; set; }
}
The ProductComponent class has two properties: int ID and string Description.
I would like the controller to work with a strongly typed view of ProductFormatModel type to create a new ProductFormatModel. The Create view should look like this:
A textfield to insert SerialID (done)
A dropdownlist to select the value of the enumerator ProductType (done)
A listbox with different rows showing the "Description" property of ProductComponent. Every row should present a record in the database. The user must be able to select one or more rows and in this way the property "Components" should have those objects in the list.
The database is a simple DbContext. MyDB.Components gives me the recordset of table "Components"
How do I pass the values of the recordset from the controller to the view?
Should I call a listboxfor with multiselectlist?
Should I add another property to the model like List ComponentsToBeSelected in which I can pass the list?
One possible design:
Use two listboxes in your view. Load the first with all possible ProductComponents that are not in model.Components, and use ListBoxFor to bind the second to the model.Components list.
Place two buttons, Add and Remove, between the lists. Wire these up with jQuery to move items between the two listboxes.

Resources