1

Topic: How to transfer in the controler the selected value from DropDownListFor (...)?

On curve I can do it through a script and an additional element of type HiddenBox (DDFor--> script--> HiddenBox--> the controler). And as  in the normal image without this chain - it is impossible to me!
The elementary :
The controler:

using System;
using System. Collections. Generic;
using System. Linq;
using System. Web;
using System. Web. Mvc;
namespace InclusiveClass. Controllers
{
public class HomeController: Controller
{
[HttpGet]
public ActionResult Index ()
{
List <City> cityList = new List <City> ();
cityList. Add (new City {Id=1, Name = "Pskov"});
cityList. Add (new City {Id=2, Name = "Moscow"});
cityList. Add (new City {Id=3, Name = "Saratov"});
cityList. Add (new City {Id=4, Name = "Vologda"});
CityListClass mod = new Controllers. CityListClass ();
mod. CityList = cityList;
return View (mod);
}
[HttpPost]
//public ActionResult Index1 (CityListClass mod)//It is empty! What to deliver???
public ActionResult Index1 (int? Id)//It is empty! What to deliver???
{
return View ("Index", "While it is unimportant 123");
}
}
public class City
{
public int Id {get; set;}
public string Name {get; set;}
}
public class CityListClass
{
public List <City> CityList {get; set;}
}
}

:

@model InclusiveClass. Controllers. CityListClass
@{
ViewBag. Title = "Index";
}
<div class = "container">
<h2> the Complex class </h2>
@using (Html. BeginForm ("Index1", "Home", FormMethod. Post))
{
<div>@Html.DropDownListFor (mod => mod. CityList, new SelectList (@Model. CityList, "Id", "Name")) </div>
<input type = "submit" value = to "Save" style = "margin-top: 5px;"/>
}
</div>

The drop down list is perfectly displayed, however, at pushing the button to argument of controler Index1 gets nothing.
In what the reason?
Yours faithfully

2

Re: How to transfer in the controler the selected value from DropDownListFor (...)?

ValGer;
Rummaged around the Internet - as it appears, a problem inveterate!

3

Re: How to transfer in the controler the selected value from DropDownListFor (...)?

ValGer wrote:

a problem inveterate!

And we live.

4

Re: How to transfer in the controler the selected value from DropDownListFor (...)?

The innocent person behind a board;
Lay down after a dinner to doze and in the sleep the solution of a problem came. It idle time also is connected with Html-racshireniem all elements... For (...).
The elementary example:
@Html. TextBoxFor (x = x. Id, htmlAttributes: new {id = "idTextBox"}) - turns to the following code:
<input data-val = "true"... id = "idTextBox" name = "Id" type = "text" value = "2"/> - (the part of parameters is lowered)
That the controler perceived this information in the list of arguments of its method the written name name = "Id" should in accuracy (It well-known, but is sometimes forgotten; - ()

[HttpPost]
public ActionResult Index1 (int? Id)
{
.................................
return View (....);
}

With the registration of the told we modify classes, the controler and representation:
Class City - without changes
Class CityListClass we add property Id which will accept the information on the identifier of the selected element of the list

public class CityListClass
{
public int Id {get; set;}//Reception of the selected element of the list
public List <City> CityList {get; set;}
}

According to told above we modify representation mod => mod. Id. The error consisted that earlier we tried to transfer in the controler all list it it is wrong!
Now after modification:

@model InclusiveClass. Controllers. CityListClass
@{
ViewBag. Title = "Index";
}
<div class = "container">
<h2> the Complex class </h2>
@using (Html. BeginForm ("Index1", "Home", FormMethod. Post))
{
<div>@Html.DropDownListFor (mod => mod. Id, new SelectList (@Model. CityList, "Id", "Name")) </div>
<input type = "submit" value = to "Save" style = "margin-top: 5px;"/>
}
</div>

Now all works normally - after pushing the button in argument of a method of controler Index1 we receive the identifier of the selected element (the picture see).

5

Re: How to transfer in the controler the selected value from DropDownListFor (...)?

ValGer;
Before using helpers - understand, how it basically works. I about http-inquiries, parameter passing and other