Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass model from one partial view to another partial view

I have a model called Result. Suppose this has 4 fields, like student_id, marks, status, remarks.
Now I have a view in which student listing is shown. In front of each student, there is a button for enter marks of exam. On clicking on button a pop-up will open and there will be 2 fields student_id, marks and 2 buttons 'pass' and 'fail'.

On clicking on fail button another pop-up will appear for enter remarks only.
Now my question is that, how can I retain values of first pop-up on second pop-up, As on clicking on 'submit' button of second pop-up, I will save all the details.

I know a way to do this using hidden fields in second pop-up. Is there any other way to do this?

Model classes are:
1. User (id, name, f_name, address...)
2. Result (student_id, marks, grade, remarks)

Student List view

@{  
List<User> Student = (List<User>)ViewData["Student"];  
}
    <table id="table_id">
      <tr>
        <th class="dbtc">S.No.</th>
           <th class="dbtc">Student Name)</th>
           <th style="width: 110px">Operate</th>
       </tr>

                @foreach (User usr in Student)
                {
                    int index = Student.IndexOf(usr);
                    <tr>
                        <td class="dbtc">
                            @(Student.ToList().IndexOf(usr) + 1)
                        </td>
                        <td>
                            @Html.ActionLink(usr.FirstName + " " + usr.LastName, "Details", "User", new { id = usr.Id }, null)
                        </td>
                        <td>
                                @Ajax.ActionLink("Examine", "Result", new { id = Model.Id, userId = usr.Id }, new AjaxOptions
                                    {
                                        HttpMethod = "GET",
                                        UpdateTargetId = "divPopup",
                                        InsertionMode = InsertionMode.Replace,
                                        OnSuccess = "openPopup('Examine Content')"
                                    })
                        </td>
                    </tr>

First Partial view of examine

@model ComiValve.Models.Result  
@using (Html.BeginForm("ExamPass", "Student", new { @id = (int)ViewBag.id, userId = (int)ViewData["UserId"] }, FormMethod.Post))
{
    <div id="divExamAdvice"></div>
    <div class="editor-label">
        @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Marks)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Grade)
    </div>
    <div class="login_submit_div">

        <input type="submit" value="Pass" />
        @Ajax.ActionLink("Fail", "ExamAdvice", new { id = (int)ViewBag.id, userId = (int)ViewData["UserId"] }, new AjaxOptions
        {
            HttpMethod = "GET",
            UpdateTargetId = "divPopup",
            OnSuccess = "openPopup('Exam Advice')"
        })
    </div>
}

Second partial view for remaks (when user click on fail, then this view will open.)

@model ComiValve.Models.ExamContent

@using (Html.BeginForm("ExamFail", "Student", new { id = Model.id }, FormMethod.Post))
{
    <div id="divExamAdvice"></div>
    <div class="editor-label">
        @Html.DisplayNameFor(model => model.Remarks)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Remarks)

    </div>
    <div class="left">
        <input type="submit" value="Confirm Fail" />
    </div>
}

Methods of Controller

    public virtual ActionResult ExamContent(int id, int userId)
            {
                ViewBag.IsApprove = true;
                ViewBag.UserId = userId;
                ViewBag.id = id;
                return PartialView("ExamContent");
            }

public virtual ActionResult ExamAdvice(int id, int userId)
        {
            ViewBag.IsApprove = true;
            if (Request.IsAjaxRequest())
            {
                Result result = new Result();
                result.id = id;
                result.User = db.Users.Find(userId);

                return PartialView("ExamAdvice", result);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
like image 780
Naman Goyal Avatar asked Nov 12 '22 21:11

Naman Goyal


1 Answers

Why are you passing the model between partial views. You can create a single Model and use it on both the views. In case of having two different tables, create the two different "Lists" of "Table" type. Like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;

namespace LearningMVCapp.Models
{
    public class Data
    {
        public List<tbl_Dept> lstDepatrment;
        public List<tbl_employees> lstEmployees;
        //other properties
    }
}

You can also use session instead of hidden fields, refer this link http://www.dotnet-tricks.com/Tutorial/mvc/906b060113-Controlling-Session-Behavior-in-Asp.Net-MVC4.html.

like image 60
Bhupendra Shukla Avatar answered Nov 15 '22 00:11

Bhupendra Shukla