简体   繁体   English

在MVC中使用2 get控制器

[英]Using 2 get controller in mvc

I need to use 2 get controller to pass data again to my view.I'm getting the data from view and sending it to second controller but ı can't pass it to view again.ViewBag.Model is load in controller ı get error when passing to view. 我需要使用2 get控制器再次将数据传递到我的视图。我从视图中获取数据并将其发送到第二个控制器,但是ı无法将其再次传递给视图.ViewBag.Model在控制器中加载ıget错误在通过查看时。

Controller: 控制器:

    [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);
    }

    [WebMethod]
    public ActionResult GetData(FormDetailViewModel model,string input)
    {
        ViewBag.model = input;

        return View(model);
    }

View: 视图:

    @using (Html.BeginForm())
    {       <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>


<h1> @ViewBag.model</h1>

JS/jQuery : JS / jQuery:

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

    $.ajax({
        url: '/User/FormDetail/GetData',
        type: "GET",
        data: { 'input': info.id },
        success: function (result) {
            console.log(result);
            //$('#ajaxDisplay').html(result);
        }
    });
});

GET http://localhost:63081/User/FormDetail/GetData?input=Notlar 500 (Internal Server Error) GET http:// localhost:63081 / User / FormDetail / GetData?input = Notlar 500(内部服务器错误)

send @ jquery.min.js:4
ajax @ jquery.min.js:4
(anonymous) @ open.js:7
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3

You are getting error just because GetData(FormDetailViewModel model,string input) GetData required two parameters in your get request you just pass input not model that's why model is null and you pass same model to view return View(model); 您因为GetData(FormDetailViewModel model,string input) GetData在您的get请求中需要两个参数而得到错误GetData(FormDetailViewModel model,string input)您只是传递了输入而不是model,这就是模型为null的原因,并且您传递了相同的模型以查看return View(model); that's cause the problem for you.you just need to pass model to your get request using Ajax otherwise do not pass model to your view. 这就是给您造成问题的原因。您只需要使用Ajax将模型传递给您的get请求,否则就不要将模型传递给您的视图。 like return View(); return View();

You are not passing data correctly, make following correction it should work after that. 您没有正确传递数据,请进行以下更正,然后它应该可以工作。

You don't need to second argument in your action method,as you are getting id in model. 您不需要在操作方法中添加第二个参数,因为您在模型中获取ID。

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

        $.ajax({
            url: '/User/FormDetail/GetData',
            type: "GET",
            data: { 'model': JSON.stringify(info), 'input': info.id },
            success: function (result) {
                console.log(result);
                //$('#ajaxDisplay').html(result);
            }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM