【问题标题】:Create Form in ASP.NET在 ASP.NET 中创建表单
【发布时间】:2018-11-24 05:02:15
【问题描述】:

我正在开发一个 ASP.NET Web 应用程序,但我不知道如何从我的表单中获取值以及如何从视图中在我的控制器上启动一个方法。

这是我的观点:

`<h1 style="text-align:center">Authentification</h1>
<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            <div class="form-horizontal" style="text-align:right">
                <h4>Veuillez saisir vos identifiants</h4>
                <div class="form-group">
                    <div class="col-md-10">
                        <input type="text" placeholder="Saisir ici votre login" size="40" runat="server" ID="UserName" name="login" CssClass="form-control" />
                    </div>
                </div>
                <div class="form-group">
                <div class="col-md-10">
                    <input type="password" placeholder="Saisir ici votre mot de passe" size="40" runat="server" ID="Password" name="mdp" TextMode="Password" CssClass="form-control" />
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="TestConnexion" class="submit" />
                </div>
            </div>
        </div>
    </section>
</div>

`

这是包含我想从位于我的视图中的表单中使用的方法的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace PhoneTeleX.Controllers
{
public class ConnexionController : Controller
{
    String login;
    String mdp;

 public ConnexionController()
    {

    }

    public ConnexionController(String login, String mdp)
    {
        this.login = login;
        this.mdp = mdp;
        this.TestInfoConnexion(login, mdp);
    }

private void TestInfoConnexion(String login, String mdp)
    {
        DAOConnexionController monDAOConnexion = new DAOConnexionController(login, mdp);
        if (monDAOConnexion.testInfoConnexion(login, mdp))
        {
           // FrmChoix frm = new FrmChoix();
            //frm.Show();
            //frm.getNomLogin(login);
            //GSBFormConnexion.ActiveForm.Close();
        }
        else
        {
            Console.WriteLine("Connexion impossible, veuillez vérifier votre login et/ou votre mot de passe");
        }

    }
}
}

目前我无法从我的输入按钮启动任何操作,我不知道为什么。

你能帮帮我吗?

【问题讨论】:

  • 您忘记了 &lt;form&gt; 元素。提交按钮提交表单,但您没有表单。您的控制器中也没有任何操作方法来提交表单 to。所以真的不清楚你期望在这里发生什么或为什么。
  • @David 谢谢你的快速回复,我会试试的
  • 旁注,没有“认证”这样的词。这是“身份验证”:-)。无论如何,如果您对所陈述的问题等基础知识感到困惑,那么您可能应该先学习微软提供的 MVC 教程,然后再继续学习,它涵盖了所有基础知识,例如创建表单、提交表单、定义模型等。然后希望你会更清楚你需要在这里做什么以及为什么。
  • @ADyson 很抱歉,但法语中存在“Authentification”这个词,我是法语^^
  • @WelGalaxy 我很抱歉。因为 SO 主要是一个英文网站,所以我认为它是英文的。但是现在我仔细查看您的 HTML,我可以看到那里还有其他法语文本。我以前在其他用英语编写的软件中看到过这种用法。所以请原谅我的错误假设。

标签: asp.net asp.net-mvc visual-studio asp.net-mvc-4 razor


【解决方案1】:

通过渲染视图打开表单(下例中的 Index.cshtml 文件)。

在 ConnexionController 中显示表单的操作:

[HttpGet]
public ActionResult Index() {

    var viewModel = new MyIndexViewModel();

    // do database access, put retrieved data into ViewModel ...

    return View("Index", viewModel);
}

&lt;form&gt;标签由“Index.cshtml”视图渲染,点击“提交”按钮后会提交给指定的动作。

Index.cshtml:

@model MyIndexViewModel

@using (Html.BeginForm("Index", "Connexion", FormMethod.Post)) {

    @* Show validation errors, if any *@
    @Html.ValidationSummary()

    @* Use MVC EditorFor to render the inputs *@
    @Html.EditorFor(m => m.Login)   
    @Html.EditorFor(m => m.Password)

    <button type="submit">Submit</button> @* This will submit the form *@
}

表单提交后,您将在 ConnexionController 中点击 POST Index 操作:

[HttpPost]
public ActionResult Index(MyIndexViewModel postdata) {
    // validate submitted data
    if (!ModelState.IsValid) {
        // show form again with validation errors
         return View("Index", postdata);
    }

    // process the submitted data ...
    var submittedLogin = postData.Login;

    // on success
    return new HttpStatusCodeResult((int) HttpStatusCode.OK); // HTTP 200
}

请注意,Web 应用程序的工作方式与 Windows 窗体应用程序不同,因为不同的步骤在不同的计算机(服务器/客户端)上运行。您将从教程中受益:Getting Started with ASP.NET MVC 5

【讨论】:

  • 实际上我的模型不是使用实体框架构建的,我只是使用 DAO 模式,所以我在 DAOConnexion 中构建我的 sql 查询,然后通过 DB_Factory 类发送它们
  • 如何填充 ViewModel 完全取决于您。重要的是您必须使用 GET - 渲染和服务页面 - POST 模式。
猜你喜欢
  • 2011-10-25
  • 1970-01-01
  • 2019-04-13
  • 2011-01-03
  • 2018-08-19
  • 1970-01-01
  • 2011-06-02
  • 2015-05-13
  • 1970-01-01
相关资源
最近更新 更多