locked
how to pass value from 2 table in my view to new controller & save in new table RRS feed

  • Question

  • User1928394251 posted

    <body>{part 1 : ( fields fill with table 1 (id,name,family,date,time)) but  (fields (modiran_name,modiran_semat) from must fill , are empty).

    question . how pass 2 diffrent section  in my view to controller & save in new table .
    @using (Html.BeginForm("index3", "afishstudio", FormMethod.Post, null))

    {

    @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" id="submit" name="submit" value="click" />
    }

    @using (Html.BeginForm("index5", "modiran", FormMethod.Post, null))

    {

    @Html.AntiForgeryToken()

    <input type="hidden" value="id" />

    <label>name=</label>
    <input type="text" name="modiran_name" asp-for="input.modiran_name" />

    <label>modirkol</label>
    <input type="radio" name="modiran_semat" value="modirkol" asp-for="input.modiran_semat" />

    <label>ghaemmagham</label>
    <input type="radio" name="modiran_semat" value="ghaemmagham" asp-for="input.modiran_semat" />

    <label>modir</label>
    <input type="radio" name="modiran_semat" value="modir" asp-for="input.modiran_semat" />

    <label>masul</label>
    <input type="radio" name="modiran_semat" value="masul" asp-for="input.modiran_semat" />

    <button type="submit" value="insert" title="ذخیره"> ذخیره </button>

    }
    </div>

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

    @Html.ActionLink("ثبت آفیش ", "../formafish/index4")

    </div>

    }

    <div>

    @Html.ActionLink("Back to List", "show")

    </div>

    </div>

    </body>

    Thursday, December 27, 2018 9:43 AM

Answers

  • User1724605321 posted

    Hi aabedeni056,

    If you should use multi  forms in one page , you could use javascript to collect information from both forms , and send to server side using Ajax . Something like :

    @using (Html.BeginForm("index3", "afishstudio", FormMethod.Post, new { id = "form1" }))
    {
    
        @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" id="submit" name="submit" value="click" />
    }
    @using (Html.BeginForm("index5", "modiran", FormMethod.Post, new { id = "form2" }))
    {
        @Html.AntiForgeryToken()
    
        <input type="hidden" value="id" />
    
        <label>name=</label>
        <input type="text" name="modiran_name" asp-for="input.modiran_name" />
    
        <label>modirkol</label>
        <input type="radio" name="modiran_semat" value="modirkol" asp-for="input.modiran_semat" />
    
        <label>ghaemmagham</label>
        <input type="radio" name="modiran_semat" value="ghaemmagham" asp-for="input.modiran_semat" />
    
        <label>modir</label>
        <input type="radio" name="modiran_semat" value="modir" asp-for="input.modiran_semat" />
    
        <label>masul</label>
        <input type="radio" name="modiran_semat" value="masul" asp-for="input.modiran_semat" />
        <button type="submit" value="insert" title="ذخیره"> ذخیره </button>
    }
    
    <div style="direction:rtl !important;margin:40px auto;width:70%">
    
        <button onclick="Submit()">ثبت آفیش </button>
    
    </div>
    

    Javascript :

    function Submit() {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url:'@Url.Action("Savevalue", "modiran")',
                    data: $('#form1').serialize()+"&"+$('#form2').serialize(),
                    success: function (data) {
                    }
                });
                window.location.href = "@Url.Action("index4", "formafish")";
            };
    

    Controller :

            [HttpPost]
            public void Savevalue(xxxtable xxxtable, string modiran_semat, modiran modiran)
            {
    
                TempData["Id"] = xxxtable.id;
                TempData["name"] = xxxtable.name;
                TempData["family"] = xxxtable.family;
                TempData["time"] = xxxtable.time;
                TempData["date"] = xxxtable.date;
                TempData["modiran_name"] = modiran.modiran_name;
                TempData["modiran_semat"] = modiran.modiran_semat;
                   
            }
            public ActionResult index4()
            {
                ViewBag.name = TempData["modiran_name"];
                ViewBag.semat =TempData["modiran_semat"];
                ViewBag.Id = TempData["Id"];
                ViewBag.Name = TempData["name"];
                ViewBag.Family = TempData["family"];
                ViewBag.Time = TempData["time"];
                ViewBag.Date = TempData["date"];
                return View();
            }
    

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 6:35 AM

All replies

  • User1724605321 posted

    Hi aabedeni056 ,

    how to pass value from 2 table in my view to new controller & save in new table

    Do you want to post the two parts of data in a single request to controller ? If yes , you should add the two parts controls in one form  , or use AJAX to get the values of all the controls , then pass to controller .

    If i misunderstand your requirement , please feel free to let me know .

    Best Regards,

    Nan Yu

    Friday, December 28, 2018 8:35 AM
  • User1928394251 posted

    hello.i have 2 @using (Html.BeginForm) in my view . i want save values in my view in new table and show in new view .

    1:=@using (html.beginform){

    @using (Html.BeginForm("index3", "afishstudio", FormMethod.Post, null))

    {

    @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" id="submit" name="submit" value="click" />
    }

    2:=1:=@using (html.beginform){

    @using (Html.BeginForm("index5", "modiran", FormMethod.Post, null))

    {

    @Html.AntiForgeryToken()

    <input type="hidden" value="id" />

    <label>name=</label>
    <input type="text" name="modiran_name" asp-for="input.modiran_name" />

    <label>modirkol</label>
    <input type="radio" name="modiran_semat" value="modirkol" asp-for="input.modiran_semat" />

    <label>ghaemmagham</label>
    <input type="radio" name="modiran_semat" value="ghaemmagham" asp-for="input.modiran_semat" />

    <label>modir</label>
    <input type="radio" name="modiran_semat" value="modir" asp-for="input.modiran_semat" />

    <label>masul</label>
    <input type="radio" name="modiran_semat" value="masul" asp-for="input.modiran_semat" />

    <button type="submit" value="insert" title="ذخیره"> ذخیره </button>

    }

    i want pass values from my view with :(to new table & show in new view )

    <div style="direction:rtl !important;margin:40px auto;width:70%" >@Html.ActionLink("ثبت آفیش ", "../formafish/index4")</div>

    please help me .

    Saturday, December 29, 2018 5:56 AM
  • User1724605321 posted

    Hi aabedeni056,

    If you should use multi  forms in one page , you could use javascript to collect information from both forms , and send to server side using Ajax . Something like :

    @using (Html.BeginForm("index3", "afishstudio", FormMethod.Post, new { id = "form1" }))
    {
    
        @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" id="submit" name="submit" value="click" />
    }
    @using (Html.BeginForm("index5", "modiran", FormMethod.Post, new { id = "form2" }))
    {
        @Html.AntiForgeryToken()
    
        <input type="hidden" value="id" />
    
        <label>name=</label>
        <input type="text" name="modiran_name" asp-for="input.modiran_name" />
    
        <label>modirkol</label>
        <input type="radio" name="modiran_semat" value="modirkol" asp-for="input.modiran_semat" />
    
        <label>ghaemmagham</label>
        <input type="radio" name="modiran_semat" value="ghaemmagham" asp-for="input.modiran_semat" />
    
        <label>modir</label>
        <input type="radio" name="modiran_semat" value="modir" asp-for="input.modiran_semat" />
    
        <label>masul</label>
        <input type="radio" name="modiran_semat" value="masul" asp-for="input.modiran_semat" />
        <button type="submit" value="insert" title="ذخیره"> ذخیره </button>
    }
    
    <div style="direction:rtl !important;margin:40px auto;width:70%">
    
        <button onclick="Submit()">ثبت آفیش </button>
    
    </div>
    

    Javascript :

    function Submit() {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url:'@Url.Action("Savevalue", "modiran")',
                    data: $('#form1').serialize()+"&"+$('#form2').serialize(),
                    success: function (data) {
                    }
                });
                window.location.href = "@Url.Action("index4", "formafish")";
            };
    

    Controller :

            [HttpPost]
            public void Savevalue(xxxtable xxxtable, string modiran_semat, modiran modiran)
            {
    
                TempData["Id"] = xxxtable.id;
                TempData["name"] = xxxtable.name;
                TempData["family"] = xxxtable.family;
                TempData["time"] = xxxtable.time;
                TempData["date"] = xxxtable.date;
                TempData["modiran_name"] = modiran.modiran_name;
                TempData["modiran_semat"] = modiran.modiran_semat;
                   
            }
            public ActionResult index4()
            {
                ViewBag.name = TempData["modiran_name"];
                ViewBag.semat =TempData["modiran_semat"];
                ViewBag.Id = TempData["Id"];
                ViewBag.Name = TempData["name"];
                ViewBag.Family = TempData["family"];
                ViewBag.Time = TempData["time"];
                ViewBag.Date = TempData["date"];
                return View();
            }
    

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 6:35 AM
  • User1928394251 posted

    hello ,Nan yu .

    thank you very much .

    i do your code ,show in new view . but i cant save in table ("formafish) . it has error on "id" .

    can i send values 2 form to controller without "id"????

    thanks.

    Sunday, January 6, 2019 9:20 AM