Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

c# - how to pass values from one view to another view

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

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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 

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...