Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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
C#MVC Razor:模拟带有验证的多页表单_C#_Asp.net Mvc_Forms_Validation_Razor - Fatal编程技术网

C#MVC Razor:模拟带有验证的多页表单

C#MVC Razor:模拟带有验证的多页表单,c#,asp.net-mvc,forms,validation,razor,C#,Asp.net Mvc,Forms,Validation,Razor,之后编辑: public Boolean Page1Complete { get; set; } public Boolean Page2Complete { get; set; } [Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")] public int? LikelyTo

之后编辑:

public Boolean Page1Complete { get; set; }
public Boolean Page2Complete { get; set; }

[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")]
public int? LikelyToReturn { get; set; }

[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page2Question1Required")]
public int? RecomendToFriend { get; set; }
if (!Model.Page1Complete)
{
    using (Html.BeginForm("PatientSatisfactionSurveyPage1", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
    {
        @for (var a = 0; a < 11; a++)
        {
           @a - @Html.RadioButtonFor(model => Model.LikelyToReturn, @a)
        }
        <input type="submit" id="page1-submit" name="page1-submit" value="Continue" class="btn green2">
   }
}
else
// Page1 was submitted successfully. Display Page 2
{
    using (Html.BeginForm("PatientSatisfactionSurveyPage2", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
    {
        @for (var a = 0; a < 11; a++)
        {
           @a - @Html.RadioButtonFor(model => Model.RecomendToFriend, @a)
        }
        <input type="submit" id="page2-submit" name="page2-submit" value="Complete" class="btn green2">
    }
}
[HttpPost]
public ActionResult PatientSatisfactionSurvey([Bind]PatientSatisfactionSurveyPage pss)
    {
        //Process and validate the first page
        if (Request.Form["page1-submit"] != null)
        {
            if (ModelState.IsValid)
            {
                pss.Page1Complete = true;
                // Page 1 Logic...
            }
        }

        //Process and validate the first page
        if (Request.Form["page2-submit"] != null)
        {
            if (ModelState.IsValid)
            {
                pss.Page2Complete = true;
                // Page 2 Logic...
            }
        }
    }
我应该把我原来的问题写得更好:在下面的例子中,我使用了两种形式:

using (Html.BeginForm....
在控制器中,如何只验证其中一个表单,而不是整个模型?这可能吗?或者,我是不是试图以一种非预期的方式使用MVC?我做ASP.NET表单的工作已经很多年了。还在学习MVC

//结束编辑

我有一个包含表单的单一视图,需要以两部分(或两页)表单的形式呈现。这两部分都有一些必填字段。我可以很好地模拟多页表单,但我遇到的问题是验证。对于每个帖子,它将验证整个视图上的所有字段。我如何让它只验证当前可见的字段

以下是我现在所做的(简化):

型号:

public Boolean Page1Complete { get; set; }
public Boolean Page2Complete { get; set; }

[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")]
public int? LikelyToReturn { get; set; }

[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page2Question1Required")]
public int? RecomendToFriend { get; set; }
if (!Model.Page1Complete)
{
    using (Html.BeginForm("PatientSatisfactionSurveyPage1", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
    {
        @for (var a = 0; a < 11; a++)
        {
           @a - @Html.RadioButtonFor(model => Model.LikelyToReturn, @a)
        }
        <input type="submit" id="page1-submit" name="page1-submit" value="Continue" class="btn green2">
   }
}
else
// Page1 was submitted successfully. Display Page 2
{
    using (Html.BeginForm("PatientSatisfactionSurveyPage2", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
    {
        @for (var a = 0; a < 11; a++)
        {
           @a - @Html.RadioButtonFor(model => Model.RecomendToFriend, @a)
        }
        <input type="submit" id="page2-submit" name="page2-submit" value="Complete" class="btn green2">
    }
}
[HttpPost]
public ActionResult PatientSatisfactionSurvey([Bind]PatientSatisfactionSurveyPage pss)
    {
        //Process and validate the first page
        if (Request.Form["page1-submit"] != null)
        {
            if (ModelState.IsValid)
            {
                pss.Page1Complete = true;
                // Page 1 Logic...
            }
        }

        //Process and validate the first page
        if (Request.Form["page2-submit"] != null)
        {
            if (ModelState.IsValid)
            {
                pss.Page2Complete = true;
                // Page 2 Logic...
            }
        }
    }
查看:

public Boolean Page1Complete { get; set; }
public Boolean Page2Complete { get; set; }

[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")]
public int? LikelyToReturn { get; set; }

[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page2Question1Required")]
public int? RecomendToFriend { get; set; }
if (!Model.Page1Complete)
{
    using (Html.BeginForm("PatientSatisfactionSurveyPage1", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
    {
        @for (var a = 0; a < 11; a++)
        {
           @a - @Html.RadioButtonFor(model => Model.LikelyToReturn, @a)
        }
        <input type="submit" id="page1-submit" name="page1-submit" value="Continue" class="btn green2">
   }
}
else
// Page1 was submitted successfully. Display Page 2
{
    using (Html.BeginForm("PatientSatisfactionSurveyPage2", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
    {
        @for (var a = 0; a < 11; a++)
        {
           @a - @Html.RadioButtonFor(model => Model.RecomendToFriend, @a)
        }
        <input type="submit" id="page2-submit" name="page2-submit" value="Complete" class="btn green2">
    }
}
[HttpPost]
public ActionResult PatientSatisfactionSurvey([Bind]PatientSatisfactionSurveyPage pss)
    {
        //Process and validate the first page
        if (Request.Form["page1-submit"] != null)
        {
            if (ModelState.IsValid)
            {
                pss.Page1Complete = true;
                // Page 1 Logic...
            }
        }

        //Process and validate the first page
        if (Request.Form["page2-submit"] != null)
        {
            if (ModelState.IsValid)
            {
                pss.Page2Complete = true;
                // Page 2 Logic...
            }
        }
    }

有很多方法可以做到这一点:

我试着做最简单的

您的控制器:

  public class LikelyToReturnModel
  {
     [Required]
     public int LikelyToReturn { get; set; }
  }

  public class RecomendToFriendModel
  {
     public int LikelyToReturn { get; set; }

     [Required]
     public int RecomendToFriend { get; set; }
  }

  public class PatientSatisfactionController : Controller
  {
     //
     // GET: /PatientSatisfaction/
     public ActionResult LikelyToReturn()
     {
        return View(new LikelyToReturnModel());
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult LikelyToReturn(LikelyToReturnModel model)
     {
        //validation example
        if (model.LikelyToReturn == 0)
        {
           ModelState.AddModelError("", "Can't be zero!!!");
        }
        if (ModelState.IsValid)
        {
           return RedirectToAction("RecomendToFriend", new { LikelyToReturn = model.LikelyToReturn });
        }
        return View(model);
     }

     public ActionResult RecomendToFriend(int LikelyToReturn)
     {
        return View(new RecomendToFriendModel { LikelyToReturn = LikelyToReturn });
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult RecomendToFriend(RecomendToFriendModel model)
     {
        if (ModelState.IsValid)
        {
           //do something
        }
        return View(model);
     }

  }
您的观点可能会返回:

  @model MVCApp.Controllers.LikelyToReturnModel
  <h2>LikelyToReturn</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.LikelyToReturn, a) @a <br />
     }
     <button type="submit">Continue</button>
  }
@model MVCApp.Controllers.LikelyToReturnModel
可能返回
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-1”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.LikelyToReturn,a)@a
} 继续 }
您的视图推荐给朋友:

  @model MVCApp.Controllers.RecomendToFriendModel
  <h2>RecomendToFriend</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()
     @Html.HiddenFor(_ => _.LikelyToReturn)

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.RecomendToFriend, a) @a <br />
     }
     <button type="submit">Complete</button>
  }
@model MVCApp.Controllers.RecomendToFriendModel
推荐朋友
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-2”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(=>wk.LikelyToReturn)
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.RecomendToFriend,a)@a
} 完成 }
有很多方法:

我试着做最简单的

您的控制器:

  public class LikelyToReturnModel
  {
     [Required]
     public int LikelyToReturn { get; set; }
  }

  public class RecomendToFriendModel
  {
     public int LikelyToReturn { get; set; }

     [Required]
     public int RecomendToFriend { get; set; }
  }

  public class PatientSatisfactionController : Controller
  {
     //
     // GET: /PatientSatisfaction/
     public ActionResult LikelyToReturn()
     {
        return View(new LikelyToReturnModel());
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult LikelyToReturn(LikelyToReturnModel model)
     {
        //validation example
        if (model.LikelyToReturn == 0)
        {
           ModelState.AddModelError("", "Can't be zero!!!");
        }
        if (ModelState.IsValid)
        {
           return RedirectToAction("RecomendToFriend", new { LikelyToReturn = model.LikelyToReturn });
        }
        return View(model);
     }

     public ActionResult RecomendToFriend(int LikelyToReturn)
     {
        return View(new RecomendToFriendModel { LikelyToReturn = LikelyToReturn });
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult RecomendToFriend(RecomendToFriendModel model)
     {
        if (ModelState.IsValid)
        {
           //do something
        }
        return View(model);
     }

  }
您的观点可能会返回:

  @model MVCApp.Controllers.LikelyToReturnModel
  <h2>LikelyToReturn</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.LikelyToReturn, a) @a <br />
     }
     <button type="submit">Continue</button>
  }
@model MVCApp.Controllers.LikelyToReturnModel
可能返回
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-1”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.LikelyToReturn,a)@a
} 继续 }
您的视图推荐给朋友:

  @model MVCApp.Controllers.RecomendToFriendModel
  <h2>RecomendToFriend</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()
     @Html.HiddenFor(_ => _.LikelyToReturn)

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.RecomendToFriend, a) @a <br />
     }
     <button type="submit">Complete</button>
  }
@model MVCApp.Controllers.RecomendToFriendModel
推荐朋友
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-2”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(=>wk.LikelyToReturn)
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.RecomendToFriend,a)@a
} 完成 }
有很多方法:

我试着做最简单的

您的控制器:

  public class LikelyToReturnModel
  {
     [Required]
     public int LikelyToReturn { get; set; }
  }

  public class RecomendToFriendModel
  {
     public int LikelyToReturn { get; set; }

     [Required]
     public int RecomendToFriend { get; set; }
  }

  public class PatientSatisfactionController : Controller
  {
     //
     // GET: /PatientSatisfaction/
     public ActionResult LikelyToReturn()
     {
        return View(new LikelyToReturnModel());
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult LikelyToReturn(LikelyToReturnModel model)
     {
        //validation example
        if (model.LikelyToReturn == 0)
        {
           ModelState.AddModelError("", "Can't be zero!!!");
        }
        if (ModelState.IsValid)
        {
           return RedirectToAction("RecomendToFriend", new { LikelyToReturn = model.LikelyToReturn });
        }
        return View(model);
     }

     public ActionResult RecomendToFriend(int LikelyToReturn)
     {
        return View(new RecomendToFriendModel { LikelyToReturn = LikelyToReturn });
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult RecomendToFriend(RecomendToFriendModel model)
     {
        if (ModelState.IsValid)
        {
           //do something
        }
        return View(model);
     }

  }
您的观点可能会返回:

  @model MVCApp.Controllers.LikelyToReturnModel
  <h2>LikelyToReturn</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.LikelyToReturn, a) @a <br />
     }
     <button type="submit">Continue</button>
  }
@model MVCApp.Controllers.LikelyToReturnModel
可能返回
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-1”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.LikelyToReturn,a)@a
} 继续 }
您的视图推荐给朋友:

  @model MVCApp.Controllers.RecomendToFriendModel
  <h2>RecomendToFriend</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()
     @Html.HiddenFor(_ => _.LikelyToReturn)

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.RecomendToFriend, a) @a <br />
     }
     <button type="submit">Complete</button>
  }
@model MVCApp.Controllers.RecomendToFriendModel
推荐朋友
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-2”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(=>wk.LikelyToReturn)
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.RecomendToFriend,a)@a
} 完成 }
有很多方法:

我试着做最简单的

您的控制器:

  public class LikelyToReturnModel
  {
     [Required]
     public int LikelyToReturn { get; set; }
  }

  public class RecomendToFriendModel
  {
     public int LikelyToReturn { get; set; }

     [Required]
     public int RecomendToFriend { get; set; }
  }

  public class PatientSatisfactionController : Controller
  {
     //
     // GET: /PatientSatisfaction/
     public ActionResult LikelyToReturn()
     {
        return View(new LikelyToReturnModel());
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult LikelyToReturn(LikelyToReturnModel model)
     {
        //validation example
        if (model.LikelyToReturn == 0)
        {
           ModelState.AddModelError("", "Can't be zero!!!");
        }
        if (ModelState.IsValid)
        {
           return RedirectToAction("RecomendToFriend", new { LikelyToReturn = model.LikelyToReturn });
        }
        return View(model);
     }

     public ActionResult RecomendToFriend(int LikelyToReturn)
     {
        return View(new RecomendToFriendModel { LikelyToReturn = LikelyToReturn });
     }

     [HttpPost]
     [ValidateAntiForgeryToken()]
     public ActionResult RecomendToFriend(RecomendToFriendModel model)
     {
        if (ModelState.IsValid)
        {
           //do something
        }
        return View(model);
     }

  }
您的观点可能会返回:

  @model MVCApp.Controllers.LikelyToReturnModel
  <h2>LikelyToReturn</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.LikelyToReturn, a) @a <br />
     }
     <button type="submit">Continue</button>
  }
@model MVCApp.Controllers.LikelyToReturnModel
可能返回
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-1”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.LikelyToReturn,a)@a
} 继续 }
您的视图推荐给朋友:

  @model MVCApp.Controllers.RecomendToFriendModel
  <h2>RecomendToFriend</h2>
  @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
  {
     @Html.ValidationSummary(true)
     @Html.AntiForgeryToken()
     @Html.HiddenFor(_ => _.LikelyToReturn)

     for (var a = 0; a < 11; a++)
     {
        @Html.RadioButtonFor(model => Model.RecomendToFriend, a) @a <br />
     }
     <button type="submit">Complete</button>
  }
@model MVCApp.Controllers.RecomendToFriendModel
推荐朋友
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“patient-satification-survey-page-2”,@class=“full form”}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(=>wk.LikelyToReturn)
对于(var a=0;a<11;a++)
{
@Html.radiobutton(model=>model.RecomendToFriend,a)@a
} 完成 }

为什么你必须使用相同的动作方法?卢卡斯,我没有。我是MVC新手。多年来一直在使用标准ASP.NET表单。我很难理解MVC是如何工作的。你能告诉我如何使用单独的行动方法,以及这将如何帮助解决我的问题吗?谢谢还有什么我可以补充我的答案,使之成为最好的答案吗?(勾选按钮)Lucas,请看我在你回答下面的问题。你总是可以使用一个表单验证单个控件或控件组,因为为什么你必须使用相同的操作方法?Lucas,我没有。我是MVC新手。多年来一直在使用标准ASP.NET表单。我很难理解MVC是如何工作的。你能告诉我如何使用单独的行动方法,以及这将如何帮助解决我的问题吗?谢谢有什么事吗