简体   繁体   中英

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.

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 :

$(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)

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); 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. like 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.

$(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);
            }
        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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