【问题标题】:how do I get user input from the view and use it in my controller如何从视图中获取用户输入并在我的控制器中使用它
【发布时间】: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


【解决方案1】:

我应该如何使用视图文件中的用户输入在控制器中声明变量 PurchaseAmount?

在这种情况下,您应该有两个模型。一个是你的User Base Model,另一个是Purchase info Model。你可以想到下面的模型。

用户购买信息模型:

public class UserPurchaseinfo
    {
        public int UserID { get; set; }
        public int PurchaseAmount { get; set; }
        public int CardFunds { get; set; }
    }

注意:因为你已经有了User Model,我应该重命名为UserPurchaseinfo以减少歧义。

修改现有用户模型:

public class UserModel
    {
        public string FullName { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string CardNum { get; set; }
        public DateTime ExpDate { get; set; }
        public string CVV { get; set; }

        public UserPurchaseinfo UserPurchaseinfo { get; set; }
    }

注意:正如您所注意到的,我们在 UserModel 类中添加了 UserPurchaseinfo,以便我们可以从同一视图获取输入。

看法:

@model DotNet6MVCWebApp.Models.UserModel

<div id="login-div">
    @using (Html.BeginForm("PostPaymetInfo", "YourControllerName", FormMethod.Post))
    {
        <table>
            <tr>
                <td></td>
                <td><strong style="text-decoration:underline;margin-left:25px;">Payment Gateway</strong></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.Username)</td>
                <td>@Html.EditorFor(model => model.Username,new { htmlAttributes = new { @class = "form-control"}})
                <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,new { htmlAttributes = new { @class = "form-control",type = "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,new { htmlAttributes = new { @class = "form-control"}})</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,new { htmlAttributes = new { @class = "form-control"}})</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,new { htmlAttributes = new { @class = "form-control"}})</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,new { htmlAttributes = new { @class = "form-control"}})</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,new { htmlAttributes = new { @class = "form-control"}})</td>
            </tr>
            <tr>
                <td></td>
                <td>@Html.ValidationMessageFor(model => model.CVV)</td>
            </tr>
            <tr>
                <td></td>
                <td><strong style="text-decoration:underline;margin-left:25px;">User Purchase Info</strong></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.UserPurchaseinfo.PurchaseAmount) </td>
                <td>@Html.EditorFor(model => model.UserPurchaseinfo.PurchaseAmount,new { htmlAttributes = new { @class = "form-control",type = "number"}})</td>
            </tr>
             <tr>
                <td>@Html.LabelFor(model => model.UserPurchaseinfo.CardFunds) </td>
                <td>@Html.EditorFor(model => model.UserPurchaseinfo.CardFunds,new { htmlAttributes = new { @class = "form-control",type = "number"}})</td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" name="name" class="btn btn-success" value="Submit" />
                </td>
            </tr>
        </table>
    }
</div>

查看输出:

控制器:

        [HttpPost]
        public ActionResult PostPaymetInfo(UserModel paymentModel)
        {
            // You can check and validate all the property which you are sending from your view
            // Now Check your PurchaseAmount here whatever you are getting on paymentModel

            return RedirectToAction("Index");
        }

最终输出:

基本上你需要将你的UserPurchaseinfo 添加到你的User Model 中,这样你就可以访问你的view 上的两个属性,最后你可以从那里获取输入并可以通过上述方式提交。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2013-04-24
    相关资源
    最近更新 更多