Save dropdownlist state after form submit in mvc - asp.net-mvc

I have a form and Html.DropDownList in it. After the form submit, dropdownlist state is changing to default. How can I maintain dropdownlist state after form submit?
#using (Html.BeginForm("Result", "Controller", FormMethod.Post, new { id = "Form" }))
{
#Html.DropDownListFor(x => x.DropList, new[] {
new SelectListItem() {Text = "first", Value = "first"},
new SelectListItem() {Text = "second", Value = "second"},
new SelectListItem() {Text = "third", Value = "third"}
}, "DefaultState")
Thanks in advance.

You cant. Unless if after submit, you are passing back the model into the view. Why would you like to do that? Because after a form submit, the form should reset and get ready for another data entry session, no?

Use the Selected property of SelectListItem
Also, make sure the value of Model.DropList is set in your controller from the post.
#using (Html.BeginForm("List", "Home", FormMethod.Post, new { id = "Form" }))
{
#Html.DropDownListFor(x => x.DropList, new[] {
new SelectListItem() {Text = "first", Value = "first", Selected = Model.DropList == "first"},
new SelectListItem() {Text = "second", Value = "second", Selected = Model.DropList == "second"},
new SelectListItem() {Text = "third", Value = "third", Selected = Model.DropList == "third"}
}, "DefaultState")

Related

Get the selected value using from dropdownlist using #Html.DropDownList

Here is the code below. the selected value senda to the controller.
#Html.DropDownList("pagesize", new List<SelectListItem>
{
new SelectListItem() {Text = "10", Value="10"},
new SelectListItem() {Text = "20", Value="20"},
new SelectListItem() {Text = "30", Value="30"},
new SelectListItem() {Text = "40", Value="40"}
}, new { onChange = string.Format("location.href = '{0}'", #Url.Action("Logs", "Logging")) })
Controller:-
public ActionResult Logs(int pagesize=10)
{
}
Remove the onChange attribute and use unobtrusive javascript. The function needs to add the selected value to the url as a route parameter.
var url = '#Url.Action("Logs", "Logging")';
$('#pagesize').change(function() {
location.href = url + '?pagesize=' + $(this).val();
}
Side note: You should be generating the SelectList in the controller, but in any case in can be simplified to
#Html.DropDownList("pagesize", new SelectList(new List<int>(){ 10, 20, 30, 40 }))

ASP .NET MVC - DropDownList OnChange

I want to hide some data when I select a value in my dropdownList.
Example :
When I choose Gender = M, I don't want to see in my Title : Mr. Only Miss or Madame.
Here is my code :
#Html.DropDownListFor(m => m.Gender, new[] {
new SelectListItem() {Text = "M", Value = "M"},
new SelectListItem() {Text = "F", Value = "F"},
}, "---Choose Gender---", new { onchange = "Select();" })
#Html.DropDownListFor(m => m.Title, new[] {
new SelectListItem() {Text = "Mister", Value = "Mr."},
new SelectListItem() {Text = "Madame", Value = "Mme."},
new SelectListItem() {Text = "Miss", Value = "Miss."}
}, "---Choose Title---")
In Javascript Section :
function Select() {
// the code.
}
You can do this with the help of JQuery,
function changeGender()
{
if($('#Gender').val()=='M')
{
$('#Title').html('');
$('#Title').html('<option value="Mister">Mr.</option>');
}
else if($('#Gender').val()=='F')
{
$('#Title').html('');
$('#Title').html('<option value="Madame">Madam</option><option value="Miss">Miss.</option>');
}
else
{
$('#Title').html('');
}
return false;
}
Hope this helps.

MVC3 model - how to make it so editorfor is a dropdown?

I am making a MVC4 webapp for peers at 2 colleges to use, and later on I will expand to other schools. In my model I currently have
public string school { get; set; }
I would like it so when I do
#Html.EditorFor(model => model.school)
It would be a drop down of "school 1" and "School 2".
How can I go about doing this cleanly? If I can, I would like to be able to make the change in the model and not have to do anything in the view besides call #Html.EditorFor. If the only way to accomplish this is do something in the view, that is OK as well.
Instead of Html.EditorFor you can use Html.DropDownListFor
#{
var items = new SelectList(new[]
{
new SelectListItem {Text = "School1", Value = "School1"},
new SelectListItem {Text = "School2", Value = "School2"},
}, "Text", "Value");
}
#Html.DropDownListFor(x => x.school, #items)
Either you can use above code or you can go with this code in your page.
#Html.DropDownListFor(model => model.Status, new List<SelectListItem>
{
new SelectListItem{ Text="New", Value = "New" },
new SelectListItem{ Text="InProcess", Value = "InProcess" },
new SelectListItem{ Text="Completed", Value = "Completed" },
new SelectListItem{ Text="Rejected",Value="Rejected"},
}, "Select Status", new { #class = "form-control select2_demo_4" })
This will surely help you out and you can get value in your model when you select any of it.

#Html.DropDownListFor How to add option?

#Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles))
The above code creates me a select list just fine. But I want to make the selection optional. Unfortunately there is no empty option and I'd like to add one in. How would I do this?
By using the proper DropDownListFor overload:
#Html.DropDownListFor(
model => model.ZipFile,
new SelectList(ViewBag.ZipFiles),
"-- please select a zip file --"
)
#Html.DropDownListFor(model => model.Country, new List<SelectListItem>
{
new SelectListItem { Text = "India", Value = "1"},
new SelectListItem { Text = "USA", Value = "2"},
new SelectListItem { Text = "Sreelanka", Value = "3"},
new SelectListItem {Text = "Africa",Value="4"},
new SelectListItem { Text = "China", Value = "5" },
new SelectListItem { Text = "Austraila", Value = "6" },
new SelectListItem { Text = "UK", Value = "7" }
}, "Select Country",
new {#Style = "Width:500px;height:40px;",
#class = "form-control input-lg"})
In the controller, when you set ViewBag.ZipFiles, add a SelectListItem to that collection.

Bind Html.DropDownList with static items

I have to bind an Html.DropDownList with just two items statically.
Text="Yes" Value="1"
Text="No" Value="0"
The important thing is that, I have to set the text and value fields.
How can I do this?
I used this is properly working
#Html.DropDownList("Status", new List<SelectListItem>
{
new SelectListItem{ Text="Active", Value = "1" },
new SelectListItem{ Text="Not-Active", Value = "0" }
})
It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData.
Example:
var list = new SelectList(new []
{
new { ID = "1", Name = "name1" },
new { ID = "2", Name = "name2" },
new { ID = "3", Name = "name3" },
},
"ID", "Name", 1);
ViewData["list"]=list;
return View();
you pass to the constratctor: the IEnumerable objec,the value field the text field and the selected value.
in the View:
<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
Code below assumes you are using razor view engine if not you will need to convert it.
#{
var listItems = new List<ListItem>();
listItems.Add(new ListItem{Text="Yes", Value="1"});
listItems.Add(new ListItem{Text="No", Value="0"});
}
#Html.DropDownListFor(m=>m.SelectedValue, listItem);
You should consider creating the model in your code instead of the view. Also this would be a good candidate for an editor template.
if you want to be alittle explicity then try
#{
var domainsList = new SelectList(new []
{
new SelectListItem { Text = ".Com", Value = ".com", Selected = true },
new SelectListItem { Text = ".Shopping", Value = ".shopping"},
new SelectListItem { Text = ".Org", Value = ".org"},
new SelectListItem { Text = ".Net", Value = ".net"},
new SelectListItem { Text = ".AE", Value = ".ae"},
new SelectListItem { Text = ".Info", Value = ".info"},
}, "Value", "Text");
}
#Html.DropDownList("TopLevelDomains", domainsList)
This solved it for me:
<td>
#{ var RlistItems = new List<SelectListItem>();
RlistItems.Add(new SelectListItem { Text = "Select Room Type", Value = "0" });
RlistItems.Add(new SelectListItem { Text = "Meeting Room", Value = "1" });
RlistItems.Add(new SelectListItem { Text = "Office", Value = "2" });
RlistItems.Add(new SelectListItem { Text = "Cafeteria", Value = "3" });
}
#Html.DropDownListFor(model=>model.FirstOrDefault().RoomType
,RlistItems,RlistItems[item.RoomType.Value].Selected=true )
</td>
<select asp-for="CountryName" asp-items=#(new List<SelectListItem> { new SelectListItem {Text="India",Value="1" } ,new SelectListItem {Text="Japan",Value="2" }} ) class="form-control">
<option>SELECT COUNTRY -- </option>

Resources