locked
how to pass data to controller action from view using jquery RRS feed

  • Question

  • User1928394251 posted

    hello.

    i have a view that get data from model.(i want send value's in my view to another view.(just show table).i dont know how write controller and view and ....

    my view is :

    @model WebApplication1.Models.afishstudio
    <script>

    var id = document.getElementById('id').value;
    var name = document.getElementById('name').value;
    var family = document.getElementById('family').value;
    var date = document.getElementById('date').value;
    var time = document.getElementById('time').value;

    function Submit() {

    $.ajax({
    type: "POST",
    url: '@Url.Action("index2")',
    data: "id=" + id
    });
    $.ajax({
    type: "POST",
    url: '@Url.Action("index2")',
    data: "name" + name
    });
    $.ajax({
    type: "POST",
    url: '@Url.Action("index2")',
    data: "family=" + family
    });
    $.ajax({
    type: "POST",
    url: '@Url.Action("index2")',
    data: "date=" + date
    });
    $.ajax({
    type: "POST",
    url: '@Url.Action("index2")',
    data: "time=" + time
    });

    return (id,name,family,date,time);
    }
    </script>
    <html>
    <head>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    </head>
    <body>
    @using (Html.BeginForm())
    {
    @Html.AntiForgeryToken()
    @Html.TextBoxFor(Model => Model.id, new { @class = "flat" })
    @Html.TextBoxFor(Model => Model.name, new { @class = "flat" })
    @Html.TextBoxFor(Model => Model.family, new { @class = "flat" })
    @Html.TextBoxFor(Model => Model.date, new { @class = "flat" })
    @Html.TextBoxFor(Model => Model.time, new { @class = "flat" })
    <input type="submit" value="تایید" />
    }

    my controller:

    public ActionResult index2(FormCollection collection)
    {
    // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;

    return View();

    }

    view:?????

    i dont know.

    please help me .



    Thursday, December 20, 2018 8:36 AM

Answers

  • User1520731567 posted

    Hi aabedeni056,

    Try this demo:

    Controller:

     public class afishstudio
           {
                public int id { get; set; }
                public string name { get; set; }
                public string family { get; set; }
    
            }
            public ActionResult PassValue_Index()
            {
                // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
                afishstudio model = new afishstudio();
                model.id = 2;
                model.name = "Ben";
                model.family = "Ben's family";
    
                return View(model);
    
            }
    
           
            [HttpPost]
            public ActionResult PassValue_Index(FormCollection collection)
            {
                // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
                var model = new afishstudio();
                TryUpdateModel(model, collection); 
    
                TempData["id"] = model.id; //using TempData to pass value from controller to controller
    
                return RedirectToAction("PassValue_Index2", "Home");
            }
    
            public ActionResult PassValue_Index2()
            {
                // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
                var aaa = TempData["id"];
    
                return View();
    
            }

    PassValue_Index View:

    @model xxx.xxx.afishstudio
    @{
        ViewBag.Title = "PassValue_Index";
    }
    
    <h2>index2</h2>
    
    
    
    <head>
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    </head>
    <body>
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.TextBoxFor(Model => Model.id, new { @class = "flat" })
            @Html.TextBoxFor(Model => Model.name, new { @class = "flat" })
            @Html.TextBoxFor(Model => Model.family, new { @class = "flat" })
            <input type="submit" value="تایید" />
        }
    
    </body>
    

    How my demo works:

    And then,use ViewBag.id in PassValue_Index2 View.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2018 8:13 AM

All replies

  • User197322208 posted

    You try to use 

    $.ajax({
    before loading 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    Jquery must be loaded before using.

    Thursday, December 20, 2018 10:37 AM
  • User1520731567 posted

    Hi aabedeni056,

    Try this demo:

    Controller:

     public class afishstudio
           {
                public int id { get; set; }
                public string name { get; set; }
                public string family { get; set; }
    
            }
            public ActionResult PassValue_Index()
            {
                // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
                afishstudio model = new afishstudio();
                model.id = 2;
                model.name = "Ben";
                model.family = "Ben's family";
    
                return View(model);
    
            }
    
           
            [HttpPost]
            public ActionResult PassValue_Index(FormCollection collection)
            {
                // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
                var model = new afishstudio();
                TryUpdateModel(model, collection); 
    
                TempData["id"] = model.id; //using TempData to pass value from controller to controller
    
                return RedirectToAction("PassValue_Index2", "Home");
            }
    
            public ActionResult PassValue_Index2()
            {
                // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
                var aaa = TempData["id"];
    
                return View();
    
            }

    PassValue_Index View:

    @model xxx.xxx.afishstudio
    @{
        ViewBag.Title = "PassValue_Index";
    }
    
    <h2>index2</h2>
    
    
    
    <head>
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    </head>
    <body>
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.TextBoxFor(Model => Model.id, new { @class = "flat" })
            @Html.TextBoxFor(Model => Model.name, new { @class = "flat" })
            @Html.TextBoxFor(Model => Model.family, new { @class = "flat" })
            <input type="submit" value="تایید" />
        }
    
    </body>
    

    How my demo works:

    And then,use ViewBag.id in PassValue_Index2 View.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2018 8:13 AM
  • User1928394251 posted

    hello.thank you .thank yow . thank you  . thank you ..........

    "my controller "

    public ActionResult index3(FormCollection collection)
    {

    var model = new afishstudio();
    TryUpdateModel(model, collection);

    TempData["id"] = model.id; //using TempData to pass value from controller to controller
    TempData["name"] = model.name;
    TempData["family"] = model.family;
    TempData["date"] = model.date;
    TempData["time"] = model.time;

    return RedirectToAction("PassValue_Index2", "afishstudio");

    }
    public ActionResult PassValue_Index2()
    {
    // resualt="id"+id,"name:"+name,"family:"+family,"date:"+date,"time:"+time;
    // var aaa = TempData["id"];
    ViewBag.id = TempData["id"];
    ViewBag.name = TempData["bame"];
    ViewBag.family = TempData["family"];
    ViewBag.date = TempData["date"];
    ViewBag.time = TempData["time"];
    return View();

    }

    """""""""""""""""""""""""my view index3 "

    <div>
    <table>
    <tr>
    <th>id:</th>
    <th>name:</th>
    <th>family:</th>
    <th>date</th>
    <th>time</th>

    </tr>
    <tr>
    <td>@ViewBag.id</td>
    <td>@ViewBag.name</td>
    <td>@ViewBag.family</td>
    <td>@ViewBag.date</td>
    <td>@ViewBag.time</td>
    </tr></table>
    </div>

    my question is :

    if i want to  add 1 opton with(m1,m2,m3   (list box or checkbox ) from another table  to my view and post  all of them to view: index 3 . i khont now . how is ?

    for examle:

    @html.textbox for .id,@html.textbox for .name,@html.textbox for .family,@html.textbox for .date,@html.textbox for .time,@html.textbox for .chekxox(m1) or m2 or m3 ???

    i khont know

    help me please ...

    Sunday, December 23, 2018 5:07 AM
  • User1928394251 posted

    hello again .

    if i use   var aaa = TempData["id"];     then must  continuse var bbb=tempdate[name] ,......

    .return view (id);  ????      that,s right ?

    what show that code?

    Sunday, December 23, 2018 7:12 AM