I have created a project in ASP.NET MVC 5 with two tables.
This is the table's structure:
CREATE TABLE [dbo].[User]
(
[UserID] INT IDENTITY (1, 1) NOT NULL,
[FirstName] VARCHAR (50) NOT NULL,
[LastName] VARCHAR (50) NOT NULL,
[Email] VARCHAR (256) NOT NULL,
[DateOfBirth] DATE NULL,
[Password] NVARCHAR (MAX) NOT NULL,
[IsEmailVerified] BIT NOT NULL,
[ActivationCode] UNIQUEIDENTIFIER NOT NULL,
[ResetPasswordCode] NVARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([UserID] ASC)
);
CREATE TABLE [dbo].[OrderDetails]
(
[OrderID] INT NOT NULL IDENTITY,
[User_ID] INT NOT NULL,
[OrderDate] DATE NULL,
[A_ChickenChop_BP] INT NOT NULL,
[A_ChickenChop_M] INT NOT NULL,
[A_Spaghetti_AH] INT NOT NULL,
[A_Spaghetti_P] INT NOT NULL,
[A_Spaghetti_S] INT NOT NULL,
[A_ChickenRice_CB] INT NOT NULL,
[A_ChickenRice_CW] INT NOT NULL,
[A_ChickenRice_D] INT NOT NULL,
[A_WantanMee_IS] INT NOT NULL,
[A_WantanMee_NS] INT NOT NULL,
CONSTRAINT [PK_OrderDetails] PRIMARY KEY CLUSTERED ([OrderID] ASC),
CONSTRAINT [FK_OrderDetails_User]
FOREIGN KEY ([User_ID]) REFERENCES [dbo].[User] ([UserID])
);
As the project is login needed so the UserID has been set up like 1,2,3,.... After login that, user can order the food from the program and I want to have a view that can display the UserID from the User table. So the code I write is like this:
public class OrderController : Controller
{
// GET: Order
[HttpGet]
public ActionResult orderUser()
{
return View();
}
[HttpPost]
public ActionResult orderUser(User orderUser)
{
using (myDatabaseEntities myDatabase = new myDatabaseEntities())
{
var userID = myDatabase.OrderDetails.Where(a => a.User_ID == orderUser.UserID).FirstOrDefault();
}
return View(orderUser);
}
}
The User_ID is the ID I want it same like the UserID from the user table. So the userid in the controller I make that orderDetails's User_ID is same to the User's UserID but it doesn't work! And the view I display in program is empty like it is read a null data.
I want make the User_ID from OrderDetails table will read the UserID table here.
Where is the mistake I make?
@model Food_Founder.Models.OrderDetail
@{
ViewBag.Title = "orderUser";
}
<h2>orderUser</h2>
@HttpContext.Current.User.Identity.Name
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>OrderDetail</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.OrderID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User_ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_ID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_ID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenChop_BP, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenChop_BP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_ChickenChop_BP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenChop_M, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenChop_M, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_ChickenChop_M, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_Spaghetti_AH, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_Spaghetti_AH, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_Spaghetti_AH, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_Spaghetti_P, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_Spaghetti_P, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_Spaghetti_P, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_Spaghetti_S, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_Spaghetti_S, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_Spaghetti_S, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenRice_CB, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenRice_CB, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_ChickenRice_CB, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenRice_CW, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenRice_CW, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_ChickenRice_CW, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_ChickenRice_D, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_ChickenRice_D, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_ChickenRice_D, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_WantanMee_IS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_WantanMee_IS, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_WantanMee_IS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.A_WantanMee_NS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.A_WantanMee_NS, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.A_WantanMee_NS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Above the code is the view code and it is like this.

As you can see there is a userid that I circle it already. I want make that it will display the data of the User's userid

var userIDin your action is assigned but never used. Currently, you are just returning the same data posted.orderUser(User orderUser)supposed to do?