【发布时间】:2022-11-21 00:47:04
【问题描述】:
我正在尝试从视图中获取用户的输入 PurchaseAmount 并将该输入用于我的逻辑,如果 PurchaseAmount > CardFunds,那么它会打印一条消息“Insufficient Funds”,如果 Purchase < CardFunds,它将处理到下一个陈述。
我在 .cs 文件中这样声明了 PurchaseAmount 和 CardFunds:
namespace Form6.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class User
{
public int UserID { get; set; }
public int PurchaseAmount { get; set; }
public int CardFunds { get; set; }
}
}
PurchaseAmount 将由用户给出,CardFunds 是一个 Int 数据类型值,以前存储在数据库中。
以下是我对控制器的看法:
namespace Form6.Controllers
{
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Authorize(Form6.Models.User userModel)
{
using (Form6Entities db = new Form6Entities())
{
var userDetails = db.Users.Where(x => x.Username == userModel.Username && x.Password == userModel.Password).FirstOrDefault();
var cardDetails = db.Users.Where(x=> x.FirstName == userModel.FirstName && x.LastName == userModel.LastName && x.CardNum == userModel.CardNum && x.ExpDate == userModel.ExpDate && x.CVV == userModel.CVV).FirstOrDefault();
var fundStatus = db.Users.Where(x => x.CardFunds == userModel.CardFunds).FirstOrDefault();
var purchaseAmount =
if (userDetails == null)
{
userModel.LoginErrorMessage = "Unauthorized for this transaction!";
return View("Index", userModel);
}
else if (PurchaseAmount > userModel.CardFunds)
{
userModel.LoginErrorMessage = "Insufficient Funds";
return View("Index", userModel);
}
else
{
Session["userID"] = userDetails.UserID;
return RedirectToAction("Index", "Home");
}
}
}
所有其他逻辑都正常工作,例如验证用户名和密码,以及所有 cardDetaills 输入 我应该如何使用视图文件中的用户输入在控制器中声明变量 PurchaseAmount
这将是视图的代码:
<div id="login-div">
@using(Html.BeginForm("Authorize", "Login",FormMethod.Post))
{
<table>
<tr>
<td></td>
<td style="text-decoration:underline">Payment Gateway</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Username)</td>
<td>@Html.EditorFor(model => model.Username)
<td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.Username)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Password) </td>
<td>@Html.EditorFor(model => model.Password)</td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.Password)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.FirstName) </td>
<td>@Html.EditorFor(model => model.FirstName)</td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.LastName) </td>
<td>@Html.EditorFor(model => model.LastName)</td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.CardNum) </td>
<td>@Html.EditorFor(model => model.CardNum)</td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.CardNum)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.ExpDate) </td>
<td>@Html.EditorFor(model => model.ExpDate)</td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.ExpDate)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.CVV) </td>
<td>@Html.EditorFor(model => model.CVV)</td>
</tr>
<tr>
<td></td>
<td>@Html.ValidationMessageFor(model => model.CVV)</td>
</tr>
<tr>
<td colspan="2">
<label class="field-validation-error">@Html.DisplayFor(model =>model.LoginErrorMessage) </label>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="name" value="Submit" />
</td>
</tr>
</table>
}
【问题讨论】:
-
你的
views 代码在哪里?你能分享一下你的看法吗 -
视图的代码已添加
-
还有什么我可以帮你的吗?
标签: c# asp.net-mvc asp.net-core asp.net-core-mvc