Reputation: 131
I want to send the selected text field in dropdown to the controller. When I use the code as follows, I can send the id number to the controllers, but the value I selected in the list pass null.
In addition , if there is a registered value in the model, I want this field to be selected in the dropdownlist when the page is opened.
Controller
public ActionResult Degerlendir(int id ,string CV_STATU)
{
using (MULAKATDBEntities1 ent = new MULAKATDBEntities1())
{
CvViewModel cv = new CvViewModel();
var entData = ent.CV.FirstOrDefault(a => a.ID_CV == id);
entData.CV_STATU = CV_STATU;
ent.SaveChanges();
}
}
In View
@using (Html.BeginForm("Degerlendir", "Home", FormMethod.Post))
{
<input type="hidden" name="id" value="@Model.Cv.ID_CV" />
@Html.DropDownListFor(model => model.Cv.CV_STATU,
new List<SelectListItem> {
new SelectListItem() { Text = "Secilmedi" },
new SelectListItem() { Text = "Kabul edildi" },
new SelectListItem() { Text = "Reddedildi" } },
new {@id="cv_statu", @class = "form-control" })
<input type="submit" id="btnSubmit" value="KAYDET" class="btn btn-add" />
}
Upvotes: 0
Views: 509
Reputation: 9151
I am assuming that what you shared on your code for your ActionResult
is already the HttpPost
Action verb. There should be a proper HttpGet
as well.
To solve your issue, make sure the first argument that you set on your DropDownListFor should match on the parameter of your Degerlendir
@Html.DropDownListFor(model => model.CV_STATU,
new List<SelectListItem> {
new SelectListItem() { Text = "Secilmedi", Value = "Secilmedi" },
new SelectListItem() { Text = "Kabul edildi", Value = "Secilmedi" },
new SelectListItem() { Text = "Reddedildi", Value = "Reddedildi", Selected = true} }, "Please select an option",
new {@id="cv_statu", @class = "form-control" })
As you notice, I changed your:
@Html.DropDownListFor(model => model.Cv.CV_STATU
To:
@Html.DropDownListFor(model => model.CV_STATU
Because what you set here will set the name
attribute on your HTML. This will be binded on your parameter on your ActionResult
of Degerlendir
particularly your parameter:
string CV_STATU
Lastly, I added Value
on your SelectListItem
, because that will be passed on your ActionResult whatever you select on your DropDownList. To set the default selected value, just add Selected = true
.
Upvotes: 0
Reputation: 1967
Set @name="cv_statu"
within the HTML helper htmlAttributes for binding to parameters on POST like this.
Also change the controller method parameter to lowercase cv_statu
. Only parameter names passed through URL routes are not case sensitive.
Upvotes: 1