【问题标题】:How Can Get Html Form value view to Controller Action Method如何获取 Html 表单值视图到控制器操作方法
【发布时间】:2018-03-03 15:24:14
【问题描述】:

这是我的输入表单,想要将输入文本值传递给控制器​​,但值仍然为空到控制器,另一个问题是无法击中我的控制器 action 当我更改控制器的名称时创建,例如 Index 我的 Action 指向这个 Action 但不能将值作为参数传递。我能做什么???

@using (Html.BeginForm("Create", "Account", FormMethod.Post, new { @id = "form-ui", @class = "form-horizontal form-label-left", @novalidate = "true", enctype = "multipart/form-data" }))

{
        <div class="form-group">
            <label for="txt_fname">First Name:</label>            
            <input type="text" class="form-control" id="txt_fname">
        </div>
        <div class="form-group">
            <label for="txt_lname">Last Name:</label>
            <input type="text" class="form-control" id="txt_lname">
        </div>
        <div class="form-group">
            <label for="txt_email">Email address:</label>
            <input type="email" class="form-control" id="txt_email">
        </div>
        <div class="form-group">
            <label for="txt_username">User Name:</label>
            <input type="text" class="form-control" id="txt_username">
        </div>
        <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control" id="pwd">
        </div>
}

My Account Controller is:

public class AccountController : Controller
    {
        // GET: Account
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string txt_fname,string txt_lname,string txt_email,string txt_username,string txt_pwd)
        {
            AccountVM account = new AccountVM();
            account._FirstName = txt_fname;
            account._LastName = txt_lname;
            account._EmailAdress = txt_email;
            account._UserName = txt_username;
            account._Password = txt_pwd;

            //Insert Value
            account.CreateAccount();
            return View();
        }
    }

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您需要将用于模型绑定和作为键值对发布的name 属性放回控制器操作。因此,将所有 input 元素更改为具有 name 属性:

    <input type="text" class="form-control" id="txt_fname" name="txt_fname">
    

    注意 name="txt_fname" ,现在你的控制器应该有一个名为 txt_name 的参数来获取其中的值。

    由于您在那里有一个视图模型,您应该按照以下方式发布模型对象。

    在您的视图上声明此视图可以绑定到的模型以进行发布和显示:

    @model YourNameSpace.ViewModels.AccountVM
    
    @using (Html.BeginForm("Create", "Account", FormMethod.Post, new { @id = "form-ui", @class = "form-horizontal form-label-left", @novalidate = "true", enctype = "multipart/form-data" }))
    
    {
            <div class="form-group">
                <label for="txt_fname">First Name:</label>            
                @Html.TextBoxFor(x=>x.txt_fname)
            .......
            ......
            </div>
    }
    

    您的操作方法现在将只有一个AccountVM 类型的参数:

    [HttpPost]
    public ActionResult Create(AccountVM account)
    {
        //Insert Value
        account.CreateAccount();
        return View();
    }
    

    您还可以在调用 CreateAccount 之前添加验证,例如:

       if(ModelState.IsValid)
       {
           //Insert Value
           account.CreateAccount();
           return View();
       }
       else
       {
           return View(account);
       }
    

    【讨论】:

    • @MuhammadSaqib 很高兴它有帮助,不要忘记接受答案:)
    • 愿上帝保佑你快乐
    • 但第 2 点不明确请回答第 2 点,当我将控制器名称索引更改为创建其未命中操作方法时????
    • 你的表格中有&lt;input type="submit" value="Create"/&gt;吗?
    • 无法点击ActionResult 它只能更改输入按钮名称
    猜你喜欢
    • 2014-07-28
    • 2019-01-17
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多