如何从一个视图值传递给另一种观点 [英] how to pass values from one view to another view

查看:92
本文介绍了如何从一个视图值传递给另一种观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很新的ASP.NET MVC使用控制器......所以这里是我的scenario.Am开发一个网上录取系统,让学生进来,并在其要求的记录填写admission.I要处理有不同的类别保存特定有关student.My学生类信息是:

am very new to ASP.NET USING MVC Controllers...so here is my scenario.Am developing an Online Admission system where students come and fill in their required records to be processed for admission.I have different classes that holds specific information about the student.My student class is:

public class Student
{
    public int ID { get; set; }
    [Required]
    public string NAME { get; set; }
    [Required]
    public string FIRSTNAME { get; set; }
    [Required]
    public string LASTNAME { get; set; }
    public virtual ICollection<Enrollment> Enrollments { get; set; }
}

我有用户在其中输入所有课程另一类称为扩招以及甲级

I have another class called Enrollments where the user Enters all the courses as well as the Grade

public class Enrollment
{
    public int EnrollmentID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }
    public Grade Grade { get; set; }
    public virtual Student Student
    {
        get;
        set;
    }
    public virtual Course Course { get; set; }
}
public enum Grade
{
    A,B,C,D,E,F
}
}

和不同的多个classes.I有一个叫做注册,在那里我会为每个类的观点的控制器。

AND different more classes.I HAVE A CONTROLLER called Registration, where i will create the views for each Classes.

因此​​,在我第一次查看它通吃学生详细信息,记录已被保存后。
当用户点击下一步,我想通过学生类我的下一个观点是招生视图的ID号,并保存记录与学生类的ID号是充当招生类的外键沿和数据库。

SO IN my first View which takes all the student Details,after the Record has been saved. when the user Clicks next,i want to pass the ID number of the student class to my next view which is the Enrollment View and save the records along with the ID number of the student class which is acting as a foreign key in the enrollment class and database.

一直在努力尝试2实现这个,但没有success.I将AP preciate一个简单的例子来演示如何做到这一点,因为使用MVC框架,我使用ASP.NET最近开始我不太好。

Have been trying so hard 2 implement this but with no success.I would appreciate a simple example to demonstrate how to achieve this because am not too good using the mvc framework as i recently started using ASP.NET.

推荐答案

首先让我们说清楚,这是MVC,而不是Web窗体。在MVC流程是这样的

First lets make it clear this is MVC and not web forms. In MVC the flow goes like this

   View ----> Controller----->Model    then back to controller and so on in usual cases.

现在你可能想要做的是通过从第一个视图到另一个视图的形式值(这里是ID),这个可能应该这样做。

Now what you probably want to do is pass a value(here ID ) from the form in first view to another view, this should probably be done like this.

请说如。您的文本存储ID名为TB1,然后编写如下code在控制器

Say for eg. your textbox storing ID is named "tb1" then write the following code in your controller

    int id  = (Request.Form["tb1"]).toString();
    //now we'll store this id in a ViewState varibale like so
    ViewData["id"] = id;
    //don't worry about the data type of ViewData["id"], it would adapt automatically 

然后适当的重定向,即从控制器第二个视图返回之后,只在你需要像这样访问此ViewState的任何变量:

Then after proper redirection, i.e. return of the second view from your controller, just access this ViewState variable any where you need like so:

   @ViewData["id"]

请注意,这里是Razor语法和你的观点需要在.cshtml网页,而不是.aspx页

Please note that this is Razor syntax and your views need to be in .cshtml pages rather than .aspx pages

这篇关于如何从一个视图值传递给另一种观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆