0

I am using MVC and want to pass values, controller to controller

My code:

public ActionResult Index()
{
    List<string> SportsName = new List<string>();
    var sport = Db.Universities.Where(ud => ud.Contact.UserName.ToLower().Trim() == User.Identity.Name.ToLower().Trim()).SingleOrDefault();
    var spt = Db.Departments.Where(i => i.UniversityID == sport.UniversityID && i.DepartmentCodeID == 4).SingleOrDefault();
    unvId = int.Parse(sport.UniversityID.ToString());
    List<Sport> dept = Db.Sports.Where(s => s.DepartmentID == spt.DepartmentID).ToList();

    foreach (var sname in dept.ToList())
    {
        var name = Db.SportsCodes.Where(s => s.SportsCodeID == sname.SportsCodeID).First();
        SportsName.Add(name.SportsName);
    }
    ViewBag.SportsName = SportsName;
    return View();
}

public ActionResult Create(string sports)
{
    ViewBag.SportsName = sports;
    int s = unvId;
    return View();
}

I want the 'sport' value in create action also. How to get the value of 'sport' in create action?

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89

2 Answers2

0

What I guess from your question is You want to pass the SportsName from Index to Create.

From Index View (.cshtml) when you call the Create method through AJAX call, pass the value of the ViewBag.Sports as a parameter.

For Example :

$('#Link').click(function () {      
    $.ajax({
        url: http://localhost/Sports/Create,
        type: 'GET',
        data: { 
                sports: "@ViewBag.SportsName" 
              },
        success: function () {
        },
        error: function () {                
        }
    });

[Note : Here it is considered that name of your controller is Sports]

This answers to your question.

Community
  • 1
  • 1
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
0

You have a number of options. If only individual action methods need that value, fetch the value in each action method that needs it:

public ActionResult SomeMethod()
{
    var sport = Db.Universities.Where(ud => ud.Contact.UserName.ToLower().Trim() == User.Identity.Name.ToLower().Trim()).SingleOrDefault();
    // ...
}

If the repeated code seems unsightly, abstract it to a helper method:

public ActionResult SomeMethod()
{
    var sport = GetSport();
    // ...
}

private SomeType GetSport()
{
    return Db.Universities.Where(ud => ud.Contact.UserName.ToLower().Trim() == User.Identity.Name.ToLower().Trim()).SingleOrDefault();
}

If it should be accessible anywhere in the class and is logically a class-level member, make it a class-level member:

private SomeType sport = Db.Universities.Where(ud => ud.Contact.UserName.ToLower().Trim() == User.Identity.Name.ToLower().Trim()).SingleOrDefault();

public ActionResult SomeMethod()
{
    // sport is accessible here
    // ...
}

Though now that I notice it, this begs the question "What is Db"? If that's a database context then it looks like you've expanded the scope of it beyond what it really should be. Database contexts and connections should be kept in as small a scope as possible. Each method that needs one should create it, use it, and destroy it. Sharing them at a larger scope without explicitly knowing what you're doing is inviting a whole host of problems. In this case, the class-level member would be initialized in the constructor:

private SomeType sport;

public YourController()
{
    using (var Db = BuildYourDBContext())
        sport = Db.Universities.Where(ud => ud.Contact.UserName.ToLower().Trim() == User.Identity.Name.ToLower().Trim()).SingleOrDefault();
}

public ActionResult SomeMethod()
{
    // sport is accessible here
    // ...
}

Any way you look at it, the point is that being a controller doesn't really make a difference. The controller is an object like any object in an object-oriented system. It shares members exactly the same way.

David
  • 208,112
  • 36
  • 198
  • 279