加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何将模型从一个局部视图传递到另一个局部视图

发布时间:2020-12-16 03:16:00 所属栏目:asp.Net 来源:网络整理
导读:我有一个名为Result的模型.假设这有4个字段,如student_id,标记,状态,备注. 现在我有一个视图,其中显示了学生列表.在每个学生面前,有一个输入考试标记的按钮.单击按钮时,将打开一个弹出窗口,将有2个字段student_id,标记和2个按钮’pass’和’fail’. 单击“失
我有一个名为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,new AjaxOptions
        {
            HttpMethod = "GET",OnSuccess = "openPopup('Exam Advice')"
        })
    </div>
}

重新生成的第二个局部视图(当用户点击失败时,此视图将打开.)

@model ComiValve.Models.ExamContent

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读