为什么我的下拉值没有提交给行动?

时间:2015-09-11 11:23:53

标签: c# razor

我的视图中有一个DropDownListFor。事实上我有3个,其中三个只有两个工作。尽管几乎完全相同的代码,我现在的处理方法是创建一个输入框并在点击按钮时使用下拉框中的值填充它(奇怪我知道,我可以使用JQuery获取值)。我已经检查过,所有名字似乎都是一样的,所以我真的不确定它为什么不提交。

查看:

<content id="GenerateReportContent" class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
    @using (Html.BeginForm("ReportSelection", "Search", FormMethod.Post, new { @id = "GenerateReportContainer" })) {

        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div class="AltFunctions">
                <ul>
                    <li>
                        <a href="javascript:document.getElementById('GenerateReportContainer').reset();" class="AltButton" id="altClearButton" title="Reset the 'Generate Report' container" >Clear</a>
                    </li>
                    <li>
                        <a href="#GRModal" data-toggle="modal" data-target="#GRModal" class="AltButton" id="GRaltInfoButton" title="Information on the 'Generate Report' container">Info</a>
                    </li>
                </ul>
            </div>

            <h1 id="GenerateReportHeader">SEARCH ENGINE</h1>
        </div>

        <input type="hidden" name="ClientID" value="@Model.ClientID" id="Client" />
        <input type="hidden" name="ClientName" value="@Model.ClientName" id="ClientName" />
        <input type="hidden" name="SupplierFound" value="@Model.SupplierFound" id="SupplierFound" />

        @Html.TextBoxFor(m => m.ClaimNo, "", new { @id = "txtGRCSelect", @class = "form-control", placeholder = "Enter Specific Claim Number..." }) 
        <br />

        <div class="ui-widget">
            @Html.TextBox("SupplierAuto", "", new { @id = "SupplierAutotxt", @class = "form-control SupplierAutoComplete", placeholder = "Search for a supplier name" })
        </div>

        @Html.DropDownListFor(m => m.SupplierID, new SelectList(Model.Suppliers, "SupplierID", "DisplayName"), "Select Supplier Name", new { @id = "SuppNameDD", @class = "GRDropDown"})

        <br />

        <!-- THE DROP DOWN IN QUESTION-->
        @Html.DropDownListFor(m => m.GroupModelClass.GroupID, new SelectList(Model.GroupModelClass.ClaimGroups, "GroupID", "GroupName"), "Select Supplier Group Name", new { @id = "SuppGroupDD", @class = "GRDropDown" })

        <br />

        @Html.DropDownListFor(m => m.ReviewPeriodID, new SelectList(Model.ReviewPeriods, "ReviewPeriodID", "ReviewPeriodName"), "Select Review Period", new { @id = "ReviewPeriodDD", @class = "GRDropDown" })

        // Have to submit this field at the moment as the drop down value is not being submitted
        <input hidden id="GroupIDInput" name="GroupIDInput" />

        <br />
        <br />
        <button type="submit" value="Submit" id="GenerateReportButton" class="btn btn-default">GO</button>

        <div id="ErrorBox" hidden>
            <div class="alert alert-danger" role="alert">
                <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                <span class="sr-only">Error:</span>
                <p id="ErrorBoxText"></p>
            </div>
        </div>

    }

</content>

控制器:

public ActionResult ReportSelection(int ClientID, string ClaimNo, string SupplierAuto, int? SupplierID = null, int? ReviewPeriodID = null, int? GroupID = null) {

            if (SupplierAuto != "") {
                var Suppliers = suppRepo.GetAllSuppliersByClientWithClaims(ClientID);
                foreach (var item in Suppliers) {
                    if (item.DisplayName == SupplierAuto) {
                        SupplierID = item.SupplierID;
                        break;
                    }
                }
                if (SupplierID == null) {
                    return RedirectToAction("Index", "Dashboard", new { ClientID = ClientID });
                }
            }

            client = clientRepo.GetClientNameByID(ClientID);


            if (SupplierID != null || ReviewPeriodID != null || GroupIDInput != null) {
                return RedirectToAction("SupplierReportSelection", new { ClientID = ClientID, SupplierID = SupplierID, ReviewPeriodID = ReviewPeriodID, ClaimIDs = ClaimIDs });
            }
            else {
                return RedirectToAction("ClaimNumberReportSelection", new { ClientID = ClientID, ClaimNo = ClaimNo });
            }

        }

任何人都知道它为什么不起作用?

2 个答案:

答案 0 :(得分:0)

使用FormCollection:

[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV,FormCollection form)
{           
  string strDDLValue = form["<your-dropdown-name>"].ToString();

  return View(MV);
}

如果需要使用模型绑定,请在模型中添加属性:

public class MobileViewModel 
{          
    public List<tbInsertMobile> MobileList;
    public SelectList Vendor { get; set; }
    public string SelectedVender {get;set;}
}

并在视图中:

@Html.DropDownListFor(m=>m.SelectedVender , Model.Vendor, "Select Manufacurer")

并在行动中:

[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{           
   string SelectedValue = MV.SelectedVendor;
   return View(MV);
}

答案 1 :(得分:0)

使用fiddler或F12进行检查,但我认为m.GroupModelClass.GroupID正在简单地传递给模型绑定器GroupID,并且它不知道它应该映射到GroupModelClass.GroupID。尝试展平你的模型吗?