我对在控制器之间传递数据并多次查看感到非常困惑

时间:2019-07-16 04:45:14

标签: c# jquery asp.net-mvc

当用户单击下拉列表中的所选项目时,我需要获取子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 });
        }

        foreach(CheckListType item in typeRep.List().ProcessResult)
        {
            TypeList.Add(new SelectListItem { Value = item.CheckListTypeId.ToString(), Text = item.CheckListType1 });
        }

        model.checkLists = TypeList;
        model.FormViewList = FormList;
        model.İtems = İtemList;

        return View(model);
}

[HttpPost]
public void Add(FormDetailViewModel model, FormCollection form, string data)
{
        Console.WriteLine(data);

        Personal personal = new Personal();
        personal.PersonalAdı = model.PersonalName;
        personal.PersonalDepartmanı = model.PersonalDepartment;
        personal.IseGırısTarihi = model.IseGirisTarihi;
        personal.PersonalId = personalRep.GetMaxId() + 1;

        model.FormDetail.PersonalId = personal.PersonalId;

        personalRep.Insert(personal);

        model.FormDetail.DetailId = fDetailRep.GetMaxId() + 1;

        fDetailRep.Insert(model.FormDetail);
}

jQuery代码:

$('#Selection').on('change', function () {
    var info = {
        id: $('#Selection option:selected').text()
    };

    $.ajax({
        url: '@Url.Action("Add", "FormDetail")',
        type: "POST",
        data: JSON.stringify(info),
        success: function (result) {
            $('#ajaxDisplay').html(result);
        }
    });
});

数据以null的形式返回。

1 个答案:

答案 0 :(得分:0)

控制器:

[HttpPost]
public JsonResult GetDataById(int id) {
    //your logic
    ....
    //response - return data which is required in your ajax success.
    return Json(response, JsonRequestBehavior.AllowGet); 
}

Js:

$('#Selection').on('change', function () {
var id = $('#Selection option:selected').text();

$.ajax({
    url: '@Url.Action("GetDataById", "FormDetail")',
    type: "POST",
    data: { 'id': id }, 
    success: function (result) {
        $('#ajaxDisplay').html(result);
    }
  });
});