美好的一天!我是ASP.NET(MVC)的新手,并尝试在更改日期值后显示一个值(来自存储过程)。这就是设计Once the date is picked, the user will click the Create button and the value should be displayed on the "Sales" textbox
的样子这是我的存储过程
CREATE PROCEDURE [dbo].[SP_DAILY_SALES]
-- Add the parameters for the stored procedure here
@Order_date date
AS
BEGIN TRANSACTION;
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
BEGIN TRY
SET NOCOUNT OFF;
DECLARE @SumTable table
(
dailySales decimal (18,2)
)
DECLARE @dailySales decimal(18,2)
SET @dailySales = (
SELECT SUM(NET_AMOUNT)
FROM [ORDER]
WHERE ORDER_DATE=@Order_date)
INSERT INTO @SumTable (dailySales)
VALUES (@dailySales)
-- Insert statements for procedure here
SELECT dailySales
FROM @SumTable
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
END CATCH;
这就是我所说的(存储库)
public bool DisplayDailySales(DateTime orderdate)
{
connection();
SqlCommand com = new SqlCommand("SP_DAILY_SALES", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@Order_date", SqlDbType.DateTime).Value = orderdate;
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
return true;
}
else
{
return false;
}
}
我的控制器
[HttpGet]
public ActionResult Statistics()
{
OrderVM dSales = new OrderVM();
var currentdate = DateTime.Now;
dSales.ORDER_DATE = currentdate;
return View();
}
[HttpPost]
public ActionResult Statistics(OrderVM order)
{
try
{
DateTime orderdate = order.ORDER_DATE;
ViewData["ORDERDATE"] = orderdate;
if (ModelState.IsValid)
{
OrderRepository orderRepo = new OrderRepository();
orderRepo.DisplayDailySales(orderdate);
}
return View();
}
catch
{
return View();
}
}
最后,在我看来,现在是这个
<div class="panel-body">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.ORDER_DATE, "Date", htmlAttributes: new { @class = "col-xs-4 col-sm-offset-1 col-sm-4" })
<div class="col-lg-6 col-lg-6 col-md-6 col-md-6 col-sm-6 col-sm-6 col-xs-6 col-xs-6">
@Html.TextBoxFor(model => model.ORDER_DATE, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date", @id = "Orderdate" })
@Html.ValidationMessageFor(model => model.ORDER_DATE, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.dailySales, "Sales", htmlAttributes: new { @class = "col-xs-4 col-sm-offset-1 col-sm-4" })
<div class="col-lg-6 col-lg-6 col-md-6 col-md-6 col-sm-6 col-sm-6 col-xs-6 col-xs-6">
@Html.TextBoxFor(model => model.dailySales, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.dailySales, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-lg-3 pull-right">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
</div>
}
</div>
希望有人能帮助我解决这个问题。谢谢!