簡體   English   中英

如何將模型從一個局部視圖傳遞到另一個局部視圖

[英]How to pass model from one partial view to another partial view

我有一個名為Result的模型。 假設這有4個字段,如student_id,標記,狀態,備注。
現在我有一個視圖,其中顯示了學生列表。 在每個學生面前,有一個輸入考試標記的按鈕。 單擊按鈕時,將打開一個彈出窗口,將有2個字段student_id,標記和2個按鈕'pass'和'fail'。

單擊“失敗”按鈕時,將顯示另一個彈出窗口,僅用於輸入備注。
現在我的問題是,如何在第二個彈出窗口中保留第一個彈出窗口的值,如同點擊第二個彈出窗口的“提交”按鈕,我將保存所有細節。

我知道在第二個彈出窗口中使用隱藏字段的方法。 有沒有其他方法可以做到這一點?

模型類是:
1.用戶(id,name,f_name,address ...)
2.結果(student_id,分數,成績,備注)

學生列表視圖

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

第一部分考察

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

重新生成的第二個局部視圖 (當用戶點擊失敗時,此視圖將打開。)

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

控制器的方法

    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");
            }
        }

為什么要在部分視圖之間傳遞模型。 您可以創建單個模型並在兩個視圖上使用它。 如果有兩個不同的表,請創建兩個不同的“表”類型的“列表”。 像這樣

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

您也可以使用會話而不是隱藏字段,請參閱此鏈接http://www.dotnet-tricks.com/Tutorial/mvc/906b060113-Controlling-Session-Behavior-in-Asp.Net-MVC4.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM