locked
why my fields in my view is empty RRS feed

  • Question

  • User1928394251 posted

    hello .

    i have view with textbox and radio button . with my controller 1: my fields value save in db but dont show in new view that i write . please help me .

    controller 1(save in db) :

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

    // return RedirectToAction("index3");
    }

    controller 2 , controller 3:

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

    }

    but dont show in my new view :

    <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>


    <div class="form-group">

    سمت :

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

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

    </div>

    </div>

    please help me

    Tuesday, December 25, 2018 10:27 AM

All replies

  • User-474980206 posted
    Your redirect to index5 does not pass any data. A redirect is used to tell the browser to load a new url. The only data passed is on the query string (part of the url).
    Tuesday, December 25, 2018 4:39 PM
  • User1520731567 posted

    Hi aabedeni056,

    I saw your code,

    You redirected multiple times with some parameters.

    index4 ->index5->PassValue_Index3

    Are you sure your parameters have values from index4 to index5?

    aabedeni056

    return RedirectToAction("index5");

    This line can not pass parameters.

    public ActionResult index5(string modiran_semat,FormCollection collection)

    I suggest you could add some breakpoints and check if the parameters(modiran_semat,collection) have values.

    I think you can combine these action to avoid multiple redirects.

    Best Regards.

    Yuki Tao

    Wednesday, December 26, 2018 5:25 AM
  • User-271186128 posted

    Hi aabedeni056,

    with my controller 1: my fields value save in db but dont show in new view that i write . please help me .

    Have you ever solved this problem?

    As bruce and Yuki Tao said, you didn't transfer data to the Index5 action method. You could modify your code as below, and transfer the modiran to "index5",

    code like this:

    return RedirectToAction("index5", modiran);

    and 

    public ActionResult index5(modiran modiran)

    Besides, in the index5 action method, you could also try to query the database and get the latest data.

    Best regards

    Tuesday, January 1, 2019 7:57 AM