Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following SQL tables and controllers:

Company
ID int PRIMARY KEY,
CompanyName varchar(100),
CompanyEmail varchar(100),
CompanyPass varchar(100)

Branch
ID int PRIMARY KEY,
BranchLocation varchar(100),
Manager varchar(100),
CompanyName varchar(100) FOREIGN KEY REFERENCES Company(CompanyName)

public ActionResult regCompany()
{}

public ActionResult regBranch()
{}

How do I display the CompanyName on the regBranch View and also save that value to the Branch table together with the BranchLocation and Manager?
Posted
Comments
Yuriy Loginov 12-Jun-13 13:03pm    
a suggestion: You should use Company.ID as the foreign key.
As for your question, could you specify how you are accessing the database, are you using entity? or some other framework? or accessing DB directly through Data Reader?
ZainNabi 12-Jun-13 13:15pm    
Im using EF and my controller looks like this:

public ActionResult RegCompany(Company c)
{
using (var db = new dbQueueing2Entities())
{
var newC = db.Companies.Create();

newC.CompanyName = c.CompanyName;
newC.CompanyEmail = c.CompanyEmail;
newC.CompanyPassword = c.CompanyPassword;

ViewBag.CoName = c.CompanyName;

db.Companies.Add(newC);
db.SaveChanges();
}
return RedirectToAction("RegBranch2", "SetupQueue");
}
Yuriy Loginov 12-Jun-13 13:31pm    
To display the company you simply add this to your view:
@ViewBag.CoName

You would save the branch information in a similar manner how you save company. Just create a new branch just like you created a new company.
ZainNabi 12-Jun-13 13:50pm    
The problem occurs when creating a view for the Branch table,I have 2 Html.TextboxFor helpers. One for BranchLocation and the other Manager.An Html.LabelFor helper that will display the CompanyName sent via ViewBag. How do I save all these values entered in the textbox and the ViewBag value to the Branch table

1 solution

Here is what you need to do.
1. create a model instead of using view bag
C#
class MyModel
{
  public string CompanyName {get; set;}
  public string CompanyEmail {get; set;}
  public string CompanyPass {get; set;}
  public string BranchLocation {get; set;}
  public string BranchManager {get; set;}
}

2. set your view to use this model
C#
@model MyModel

3. set your model fields inside your controller
C#
public class MyController : Controller
{
  MyModel myModelObj;
  public ActionResult regCompany()
  {
    myModelObj = new MyModel();
    myModelObj.CompanyName = "Some company";
    .....
    .....
    return RedirectToAction("regBranch", "SetupQueue", new {companyName = CompanyName ...}
  }

  public ActionResult regBranch(string CompanyName....)
  {
      var companyName = CompanyName;
  }
}

This is the general idea of how it should be done if you are trying to pass values between two controllers. If you need to display the model inside the view and then pass values to your controller the the controller should return the View object and not RedirectToAction. So you would do this
C#
public ActionResult regCompany(){
return View(myModelObj);}

and inside the view
C#
@Html.TextBoxFor(model => model.BranchManager)

and finally the controller that will receive the values form the view
C#
regBranch(string BranchManager....)
{
  var barnchManager = BranchManager
}
 
Share this answer
 
v2
Comments
ZainNabi 13-Jun-13 5:08am    
Thank you.
ZainNabi 13-Jun-13 5:15am    
Question 2

Im trying to create a dynamic numer of textboxes based on the value that the user enters. How do I go about doing this?

Yours Faithfully
Yuriy Loginov 13-Jun-13 8:58am    
you will need to save the number of textboxes in your model, just like you did for the branch manager. Then inside your view you need a loop like this:

@foreach (int i = 0; i < model.numberOfTextBoxes; i++)
<input type="text"><br>
ZainNabi 14-Jun-13 2:19am    
Great solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900