Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 文本框赢得';不要变红_Javascript_Asp.net Mvc - Fatal编程技术网

Javascript 文本框赢得';不要变红

Javascript 文本框赢得';不要变红,javascript,asp.net-mvc,Javascript,Asp.net Mvc,其中一个文本框的cshtml代码。我试图在表单提交上触发一个函数,检查文本框中是否键入了任何内容。如果文本框中没有键入任何内容,则边框应变为红色 @model WebForum.Models.AccountViewModel <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src

其中一个文本框的cshtml代码。我试图在表单提交上触发一个函数,检查文本框中是否键入了任何内容。如果文本框中没有键入任何内容,则边框应变为红色

@model WebForum.Models.AccountViewModel

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="~/Scripts/Custom.js"></script>
</head>



<h2 style="font-size:larger">CreateAccount</h2>

<p style="font-size:large">Please enter BOTH an account name and a password.</p>

<div id="account_create">

    @using (Html.BeginForm("AccountCreated", "Home", FormMethod.Post, new { @id = "accountform" }, )) 
    {

        @Html.TextBoxFor(Model => Model.Account_Name, new { @id = "account_name", @placeholder = "Your Account Name" })
        <br /> 
        <br />
        @Html.TextBoxFor(Model => Model.Password, new { @id = "account_password", @placeholder = "Type Password Name" })
        <br />
        <br />
        @Html.ValidationMessageFor(m => Model.Account_Name)
        @Html.ValidationMessageFor(m => Model.Password)
        <form id="accountform" onsubmit="accountform()">
            <input type='submit' id="accountform" name="Create Account">
        </form>

            }

</div>
这是控制器代码,但这不应该太重要:

[HttpPost]
public ActionResult AccountCreated(Models.AccountViewModel model)
{

    String Account_Name = model.Account_Name;
    String Account_Password = model.Password;


    if (!ModelState.IsValid)
    {
        return RedirectToAction("CreateAccount");
    }

    //if (Account_Name == null || Account_Password == null)
    //    return RedirectToAction("CreateAccount");

    WebForumEntities2 db = new WebForumEntities2();

    foreach(Account eachAccount in db.Accounts)
    {
        if (eachAccount.Account_Name.Equals(Account_Name))
        {
            return RedirectToAction("AccountCreatedMessage");
        }
    }

    int nextiD = 0;
    if (db.Accounts.ToList().Count > 0)
    {
        nextiD = db.Accounts.ToList().Last().Id + 1;
    }

    var newAccount = new Account();

    newAccount.Account_Name = Account_Name;
    newAccount.Password = Account_Password;
    newAccount.Id = nextiD;

    db.Accounts.Add(newAccount);
    db.SaveChanges();

    return RedirectToAction("CreateAccount");

}
请帮我把文本框变成红色

@model WebForum.Models.AccountViewModel

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="~/Scripts/Custom.js"></script>
</head>



<h2 style="font-size:larger">CreateAccount</h2>

<p style="font-size:large">Please enter BOTH an account name and a password.</p>

<div id="account_create">

    @using (Html.BeginForm("AccountCreated", "Home", FormMethod.Post, new { @id = "accountform" }, )) 
    {

        @Html.TextBoxFor(Model => Model.Account_Name, new { @id = "account_name", @placeholder = "Your Account Name" })
        <br /> 
        <br />
        @Html.TextBoxFor(Model => Model.Password, new { @id = "account_password", @placeholder = "Type Password Name" })
        <br />
        <br />
        @Html.ValidationMessageFor(m => Model.Account_Name)
        @Html.ValidationMessageFor(m => Model.Password)
        <form id="accountform" onsubmit="accountform()">
            <input type='submit' id="accountform" name="Create Account">
        </form>

            }

</div>
  • 我尝试过调试,但从未调用javascript函数

首先,我将使用数据属性进行字段验证

public class AccountViewModel
{
  [Required] // data attribute
  public string Account_Name;

  [Required]
  public string Password;
}
然后,您可以正常提交表格。如果ASP.NET引擎检测到任何数据验证错误,它会将类
。字段验证错误
放在您的输入上

您可以根据自己的喜好设置此类的样式,如下所示:

.field-validation-error
{
  border-color: red;
}
其他一些注意事项:

  • 如果有一个表单嵌套在另一个表单中,我将删除该表单 在你的按钮周围
  • 不知道为什么要将输入放入字符串中 在控制器中,而不是使用ViewModel
  • 我通常将DbContext放在ActionResults之外,请确保将其处理掉

首先,我将使用数据属性进行字段验证

public class AccountViewModel
{
  [Required] // data attribute
  public string Account_Name;

  [Required]
  public string Password;
}
然后,您可以正常提交表格。如果ASP.NET引擎检测到任何数据验证错误,它会将类
。字段验证错误
放在您的输入上

您可以根据自己的喜好设置此类的样式,如下所示:

.field-validation-error
{
  border-color: red;
}
其他一些注意事项:

  • 如果有一个表单嵌套在另一个表单中,我将删除该表单 在你的按钮周围
  • 不知道为什么要将输入放入字符串中 在控制器中,而不是使用ViewModel
  • 我通常将DbContext放在ActionResults之外,请确保将其处理掉