Der Dropdown-Listenwert ist nach der Veröffentlichung auf dem Controller in ASP.NET MVC nullC#

Ein Treffpunkt für C#-Programmierer
Guest
 Der Dropdown-Listenwert ist nach der Veröffentlichung auf dem Controller in ASP.NET MVC null

Post by Guest »

Ich bin neu bei ASP.NET MVC. Ich habe ein einfaches Formular mit einer Schaltfläche zum Senden und einem HTML-Auswahlelement, das mit zwei Elementen gefüllt ist. Wenn das Formular an meinen Controller gesendet wird, sind alle Formularwerte null. Ich habe es stattdessen sogar mit $.POST versucht und die ID-Variable, die ich sende, ist null, wenn sie den Controller erreicht. Hier ist mein Code:

HTML

Code: Select all

 @using (Html.BeginForm("SetOptionForUser", "MyController", FormMethod.Post, new { @class="form-inline" }))
{
@Html.AntiForgeryToken()



@foreach (var item in Model.Persons)
{
@item.Name
}


Go

}
MVC-Controller

Code: Select all

[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult SetOptionForUser(FormCollection form)
{
string option = form["ddlSelect"].ToString(); //null error
return RedirectToAction("AnotherAction", "AnotherController");
}
Es scheint, dass nichts im Formular gesendet wird. Ich habe auch Folgendes versucht:

JS

Code: Select all

$("#btnEnter").click(function (e) {
var optionId = $("#ddlSelect").val(); //this get val correctly
$.post(@Url.Action("SetOptionForUser", "MyController"), optionId);
});
MVC-Controller für JS-Methode

Code: Select all

**MVC Controller**

[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult SetOptionForUser(int optionId) //null
{
string option = optionId.ToString(); //null error
return RedirectToAction("AnotherAction", "AnotherController");
}
Was mache ich falsch?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post