Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Asp.net mvc 为什么提交前会显示我所需的信息?_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 为什么提交前会显示我所需的信息?

Asp.net mvc 为什么提交前会显示我所需的信息?,asp.net-mvc,Asp.net Mvc,我有这个控制器来创建新的客户类型。代码工作,它创建新客户,发送错误,若用户创建同一客户两次 但问题是,在开始的时候(当用户进入表单时),它已经警告我的字段是必需的!我想在提交后显示required(如果输入字段为空) 我的模型是: public class CustomerType { public int IdNewType { get; set; } [Required(ErrorMessage = "Customer is required!")] public

我有这个控制器来创建新的客户类型。代码工作,它创建新客户,发送错误,若用户创建同一客户两次

但问题是,在开始的时候(当用户进入表单时),它已经警告我的字段是必需的!我想在提交后显示required(如果输入字段为空)

我的模型是:

public class CustomerType
{
    public int IdNewType { get; set; }

    [Required(ErrorMessage = "Customer is required!")]
    public string CustomType { get; set; }

}
控制器为:

public ActionResult CreateNewCustomerType(Models.CustomerType customerType)
{
try
{
  using (var dataC = new userDbEntities())
  {
     var sysCustomerType = dataC.CustomTypes.Create();

     if (sysCustomerType != null)
     {
        sysCustomerType.CustomerType = customerType.CustomType;

        var count = dataC.CustomTypes.Count(c => c.CustomerType == customerType.CustomType);
        if (count == 0)
        {
           dataC.CustomTypes.Add(sysCustomerType);
           dataC.SaveChanges();
           return RedirectToAction("CustomerTypeView", "LoginAdmin");
         }
         else
         {
            ModelState.AddModelError("CustomType", "This customer alerady exists!");
         }
      }
      else
      {
         ModelState.AddModelError("CustomType", "");
      }
   }
}
catch (Exception ex)
{
   string error = ex.Message;
}
return View(customerType);
}  
鉴于此,我使用它来显示错误:

@model AdminRole.Models.CustomerType
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()  
    <label class="control-label" for="us">Stranka</label>
        @Html.HiddenFor(model => model.IdNewType)
        @Html.TextBoxFor(u => u.CustomType, new{@class="form-control", id="us"})

    <span>@Html.ValidationSummary(false, null, new{@class="alert alert-danger alert-dismissible", role="alert"})</span>

        <input class="btn btn-default pull-left btn-success" type="submit" value="Create"/>

}
@model AdminRole.Models.CustomerType
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
施特兰卡
@Html.HiddenFor(model=>model.IdNewType)
@TextBoxFor(u=>u.CustomType,新的{@class=“form control”,id=“us”})
@ValidationSummary(false,null,新{@class=“alert-alert-danger-alert-dismissible”,role=“alert”})
}
谢谢你的帮助

public ActionResult CreateNewCustomerType()
{
    return view();
}

[HttpPost]
public ActionResult CreateNewCustomerType(Models.CustomerType customerType)
{
    /* ... */
}

像这样试试。我想这会解决您的问题。

您能在控制器上同时显示GET和POST操作吗?:)等等@Tobias,这是我这部分的全部控制器。。。我应该再吃点什么吗。。。这是一个简单的创建表单,你需要get和post吗?看来@Tobias我的代码需要post和get。