在mvc 5中的一个控制器上使用两个表单处理视图

时间:2016-01-06 07:08:52

标签: asp.net-mvc entity-framework

我有一个下拉框。根据所选项目的下拉框我想在同一视图下面生成表格,请找到下面的代码。

[HttpGet]
public ActionResult BindDropDown()
{
    ViewBag.doctype = new SelectList(db.DocTypeMasters, "Id", "DocTypeName");


    return View();
}

[HttpPost]
public ActionResult BindDropDown(string DocType)
{
    String Doc = DocType.Trim();
    return View();
}

这是我的观看代码

@model C3CardKYC.Models.DocTypeMaster

@{
    ViewBag.Title = "BindDropDown";
}


<h2>BindDropDown</h2>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $('#MyIdAndName').change(function () {

            var DocType = $("#MyIdAndName option:selected").text();

            alert(DocType);
            $.ajax({
                url: '@Url.Action("BindDropDown", "Document")',
                type: 'POST',
                data: { DocType: DocType },
                success: function (result) {
                    alert('success')
                }

            })
        });
    });
</script>



        <fieldset>
            <legend>DocTypeMaster</legend>
        @*Select Document Type: @Html.DropDownList("doctype", "Select");*@
        @Html.DropDownList("MyIdAndName", ViewBag.doctype as SelectList, " -- Select Document Type -- ")
           </fieldset>
        <p id="hi">
        @Html.ActionLink("Back to List", "Index")
        </p>
<div id="hi">
    @using (Html.BeginForm("BindDropDown", "Document", FormMethod.Post, new { id = "submitForm" }))
    {
        <fieldset>

            <ol>
                <li>
                    @Html.LabelFor(Model => Model.DocTypeName)
                    @Html.TextBoxFor(Model => Model.DocTypeName, new { maxlength = 50 })
                    @Html.ValidationMessageFor(Model => Model.DocTypeName)
                </li>

            </ol> <button type="submit" id="btnSave" name="Command" value="Save">Save</button>
            <button type="submit" id="btnSubmit" name="Command" value="Submit">Submit</button>
            <button type="submit" id="btnCancel" name="Command" value="Cancel" onclick="$('#submitForm').submit()">Cancel (Server Side)</button>
        </fieldset>
    }
</div>

当我在下面运行时,错误即将来临。 没有类型&#39; IEnumerable&#39;的ViewData项目。有密钥&#39; MyIdAndName&#39;。

如果我选择项目frm下拉列表,则只显示下面的表格。请建议

0 个答案:

没有答案