locked
how to show (textbox & radio button ) after save in view RRS feed

  • Question

  • User1928394251 posted

    i have view :

    @using (Html.BeginForm("index4", "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>
    }

    ****************************when click on submit(textbox & radio button selection ) save in db but dont show in my new view.**********************************

    my controller:(+++++++++++++ do  save  ++++++++++++++++++)

    public ActionResult index4(string modiran_semat, modiran modiran)
    {
    TryUpdateModel(modiran, modiran_semat);

    if (ModelState.IsValid)
    {
    db.modiran.Add(modiran);
    db.SaveChanges();
    return RedirectToAction("index5");

    }
    return View();


    }

    **********************dont view **************************

    public ActionResult index5(string modiran_semat,FormCollection collection)
    {
    // var semat = Request.Form["modiran_semat"];
    var model = new modiran();
    TryUpdateModel(model, collection);
    // TryUpdateModel(model, modiran_semat);
    TempData["modiran_id"] = model.modiran_id;
    TempData["modiran_name"] = model.modiran_name;
    TempData["modiran_semat"] = model.modiran_semat;
    return RedirectToAction("PassValue_Index3", "modiran");
    }

    public ActionResult PassValue_Index3()
    {

    ViewBag.modiran_id = TempData["modiran_id"];

    ViewBag.modiran_name = TempData["modiran_name"];

    ViewBag.modiran_semat = TempData["modiran_semat"];


    return View();

    }

    *************************************

    my new view is :

    <div class="form-group">

    id:

    <div class="col-md-10">

    @Html.TextBox("id", (Int32)ViewBag.modiran_id, new { @style = "width: 300px;" })

    </div>

    </div>


    <div class="form-group">

    نام :

    <div class="col-md-10">

    @Html.TextBox("name", (string)ViewBag.modiran_name, new { @style = "width: 300px;" })

    </div>

    </div>

    but my fields in new view is empty !!!!!!!!!!!!

    Tuesday, December 25, 2018 10:20 AM

Answers

  • User1724605321 posted

    Hi aabedeni056 ,

    That seems you redirect to indext5 and then redirect to PassValue_Index3 . You can use TempData to pass value to another action while using RedirectToAction . Something like :

      public ActionResult index4(string modiran_semat, modiran modiran)
            {
                TempData["name"] = modiran.modiran_name;
                TryUpdateModel(modiran, modiran_semat);
                if (ModelState.IsValid)
                {
                    db.modiran.Add(modiran);
                    db.SaveChanges();
                    return RedirectToAction("index5");
                }
                return View();
            }
            public ActionResult index5()
            {
                string modiran_name = (String)TempData["name"];
                var model = db.modiran.First(m => m.modiran_name == modiran_name);
                TempData["modiran_id"] = model.modiran_id;
                TempData["modiran_name"] = model.modiran_name;
                TempData["modiran_semat"] = model.modiran_semat;
                return RedirectToAction("PassValue_Index3", "modiran");
            }
    
            public ActionResult PassValue_Index3()
            {
                ViewBag.modiran_id = TempData["modiran_id"];
                ViewBag.modiran_name = TempData["modiran_name"];
                ViewBag.modiran_semat = TempData["modiran_semat"];
                return View();
    
            }
    

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 26, 2018 8:16 AM