猿问

我对在控制器和视图之间多次传递数据感到非常困惑

当用户单击下拉列表的所选项目时,我需要获取子 id,并且人们告诉我,我需要在控制器和视图之间传递数据多次,我找到一个 ajax 代码将该 id 发布到控制器,但它返回 null。


这是执行此操作的正确方法吗?


我的看法 :


@using (Html.BeginForm())

{

<table class="table table-hover">

    <tr>

        <td>

            @Html.Label("PersonalAdı")

        </td>

        <td>@Html.EditorFor(model => model.PersonalName)</td>

    </tr>

    <tr>

        <td> @Html.Label("Departman")</td>

        <td> @Html.EditorFor(model => model.PersonalDepartment)</td>

    </tr>

    <tr>

        <td>@Html.Label("IseGirisTarihi")</td>

        <td>@Html.EditorFor(model => model.IseGirisTarihi)</td>

    </tr>


    <tr>

        <td>

            @Html.DisplayNameFor(model => model.FormDetail.Form)

        </td>

        <td>

            @Html.DropDownListFor(model => model.FormDetail.FormId, 

          Model.FormViewList)

        </td>

    </tr>

    <tr>

        <td>

            @Html.DisplayNameFor(model => model.FormDetail.CheckListType)

        </td>

        <td>

            @Html.DropDownListFor(model => 

  model.FormDetail.CheckListTypeId, Model.checkLists, new 

 {@id="Selection", 

 @class = "drop-open" })

        <td><button onclick="">Bolum Ekle</button></td>

    </tr>

    <tr>

        <td colspan="2" style="padding-top:40px;">

            <input type="submit" value="Save" class="btn btn-info" />

        </td>

    </tr>

</table>

<script src="~/Scripts/jquery-3.1.1.min.js"></script>

<script src="~/Content/js/open.js"></script>

}

我的控制器:


[HttpGet]

public ActionResult Add()

{

        FormDetailViewModel model = new FormDetailViewModel();


        foreach(Form item in formRep.List().ProcessResult)

        {

            FormList.Add(new SelectListItem { Value = item.FormId.ToString(), Text = item.TeslimEden });

        }


        foreach(Item item in itemRep.List().ProcessResult)

        {

            İtemList.Add(new SelectListItem { Value = item.ItemId.ToString(), Text = item.ItemDesc });

        }


哆啦的时光机
浏览 104回答 1
1回答

呼啦一阵风

控制器 :[HttpPost]public JsonResult GetDataById(int id) {&nbsp; &nbsp; //your logic&nbsp; &nbsp; ....&nbsp; &nbsp; //response - return data which is required in your ajax success.&nbsp; &nbsp; return Json(response, JsonRequestBehavior.AllowGet);&nbsp;}杰斯:$('#Selection').on('change', function () {var id = $('#Selection option:selected').text();$.ajax({&nbsp; &nbsp; url: '@Url.Action("GetDataById", "FormDetail")',&nbsp; &nbsp; type: "POST",&nbsp; &nbsp; data: { 'id': id },&nbsp;&nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; $('#ajaxDisplay').html(result);&nbsp; &nbsp; }&nbsp; });});
随时随地看视频慕课网APP
我要回答