locked
how to send jquery value to controller and then view RRS feed

  • Question

  • User1928394251 posted

    i want send (textbox for ) value to another view.

    my view:

    @model WebApplication1.Models.afishstudio
    @{
    ViewBag.Title = "تایید مدیریت";
    }

    <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 true;
    }
    </script>
    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/bootstrap-rtl.min.css" rel="stylesheet" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    </head>
    <body>



    <div class="panel panel-primary">
    <div style="direction:rtl !important;margin:40px auto;width:70%">
    <h2> ورود به صفحه تایید آفیش </h2>
    </div>
    </div>

    <div>
    <div class="panel panel-body">

    <div style="direction:rtl !important;margin:40px auto;width:70%">


    @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="تایید" />
    }

    <form method="post" action="">
    <select name="Choice" dir="rtl">
    <option value="1">1</option>
    <option value="2">2 </option>
    <option value="3">3</option>
    <option value="1">4</option>
    <option value="2">5</option>
    <option value="3">6</option>
    </select>
    <input type="submit" value="تاييد">
    </form>
    <div>
    @Html.ActionLink("Back to List", "show")
    </div>
    </div>
    </div>
    </div>
    </body>
    </html>

    my controller :

    i dont know how to write controller for send value's from past value to next value

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

    return View();

    }

    my new view:

    @model WebApplication1.Models.afishstudio

    @{
    Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index2</title>
    </head>
    <body>
    <div>
    <div>
    <dl>
    <dt>id:</dt>
    <dd>@ViewBag.id</dd>
    <dt>name:</dt>
    <dd>@ViewBag.name</dd>
    <dt>family:</dt>
    <dd>@ViewBag.family</dd>
    <dt>date:</dt>
    <dd>@ViewBag.date</dd>
    <dt>time:</dt>
    <dd>@ViewBag.time</dd>
    </dl>
    </div>
    </div>
    </body>
    </html>

    i dont khnow  write it .

    i want to help

    thanks

    Thursday, December 20, 2018 7:31 AM

All replies

  • User1520731567 posted

    Hi aabedeni056,

    I saw your TextBoxFor in a form,so I know you want to use submit form to pass value from view to controller.

    In this way, you should note that the name of html tags which you need to pass.

    And these tags should be coded in @using (Html.BeginForm()).

    The type of submit button should be 'submit'.(here you are right).

    When you click the submit button 'تایید':

    The Compiler will jump to index2 action:

    public ActionResult index2(int id, string name, string family, int date, int time)//you can get value by these parameters
    {
      //you could also define ViewBag to pass value
    ViewBag.id=id;//and call ViewBag.id directly in other view and action //or TempData also could pass controller to controller
    return View();//this line will jump to the original page

    //if you would like to the other page,you need to redirect to other action,like:
    return View("ActionName"); write the name of other action
    //or
    return RedirectToAction("ActionName", "ControllrtName",new { id= id,name=name,...}); //also pass parameters to the other action
    }

    Here the example how to submit form in mvc,you could refer to:

    https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx

    Actually,there are many ways to pass value,this is the way using ajax not submit form,you could refer to:

    https://www.aspsnippets.com/Articles/ASPNet-MVC-Call-Controller-Method-from-View-using-jQuery-AJAX.aspx

    Best Regards.

    Yuki Tao

    Thursday, December 20, 2018 9:08 AM
  • User-2054057000 posted

    Hello, 

    With jQuery you will call the Controller's Action only and then the Action will return to any View of it's choice. 

    Now to send values from jQuery to Action method you use the data parameter of the .ajax() method of jQuery.

    Kindly check this reference tutorial - Exposing Multiple Ways to call Partial Views in ASP.NET MVC – with codes

    thanks and regards

    Thursday, December 20, 2018 1:02 PM