0

2 つのオプションがあるサインアップ フォームがあります。

  1. 個人的
  2. 会社

それらのフィールドの一部は同じですが、パスワード、ユーザー名、電子メールなどは異なります。

私の質問は、その場合にモデルのどの戦略を実装する必要があるかということです。

つまり、2つの「TAB」と1つのボタン「SUBMIT」を使用していますが、その場合、UIフィールドが重複しています...そして、その場合にクラスMODELがどうあるべきか、どのように検証する必要があるのか​​ わかりません.. .

または...

2つのボタン「SUBMIT」を使用し、何とか2つのモデルを使用する必要があります....

if (コードは次のとおりです)を使用できることはわかっていますが、必要なモデルはどれですか?

<html>
...
  <body> 
    <div>

      <form action="/SignUp/Personal" method="post">
        <input type="text" name="username" value="" />
        <input type="text" name="passowrd" value="" />
        <input type="submit" name="signup" value="SUBMIT" />
      </form>


      <form action="/SignUp/Company" method="post">
        <input type="text" name="username" value="" />
        <input type="text" name="passowrd" value="" />
        <input type="submit" name="signup" value="SUBMIT" />
      </form>
    </div>
</body>
</html>

うーん、どのアプローチを使用できるかわかりません...

どんな手掛かり?

ありがとうございました!!!

4

1 に答える 1

1

いくつかのアプローチがありますが、UIフィールドを複製せず、単一の送信ボタンを持つことができるアプローチが存在します。選択したユーザーAccountTypeに応じてモデル検証を分割できます。カスタムActionMethodSelectorAttributeは、ユーザーアカウントタイプに応じてメソッドを分割するのに役立ちます。モデルは、適切なアクションで自動的に検証されます。

実装例は次のとおりです。

コントローラ:

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new SignUpModel
                            {
                                Common = new Common(),
                                Personal = new Personal(),
                                Company = new Company()
                            });
        }



        [HttpPost]
        [SignUpSelector]
        [ActionName("Index")]
        public ActionResult PersonalRegistration(Personal personal, Common common)
        {
            if (ModelState.IsValid)
            {
                //your code
            }
            return View("Index", new SignUpModel()
                                     {
                                         Common = common,
                                         Personal = personal,
                                         Company = new Company()
                                     });
        }

        [HttpPost]
        [SignUpSelector]
        [ActionName("Index")]
        public ActionResult CompanyRegistration(Company company, Common common)
        {
            if(ModelState.IsValid)
            {
                //your code
            }
            return View("Index", new SignUpModel()
                                     {
                                         Common = common,
                                         Personal = new Personal(),
                                         Company = company
                                     });
        }


    }

モデル:

public class SignUpModel
{
    public string AccountType { get; set; }
    public Common Common { get; set; }
    public Company Company { get; set; }
    public Personal Personal { get; set; }
}


public class Company
{
    [Required]
    public string CompanyName { get; set; }
    public string Address { get; set; }
}

public class Personal
{
    [Required]
    public string FirstName { get; set; }
    public int Age { get; set; }
}

public class Common
{
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Passwrod { get; set; }
}

カスタムActionMethodSelectorAttribute:

public class SignUpSelector : ActionMethodSelectorAttribute
{
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
         return (controllerContext.HttpContext.Request.Form["AccountType"] + "Registration" == methodInfo.Name);
    }
}

見る:

@model MvcModelValidationTest.Controllers.SignUpModel 

@{
    ViewBag.Title = "Home Page";
}

@using(Html.BeginForm())
{
    @Html.Display("Personal")
    @Html.RadioButtonFor(x=>x.AccountType, "Personal",new { @checked = "checked" })<br/>

    @Html.Display("Company")
    @Html.RadioButtonFor(x=>x.AccountType, "Company")<br/>

    @Html.TextBoxFor(x=>x.Common.UserName)<br/>
    @Html.PasswordFor(x=>x.Common.Passwrod)<br/>

    @Html.TextBoxFor(x=>x.Company.CompanyName)<br/>

    @Html.TextBoxFor(x=>x.Company.Address)<br/>

    @Html.TextBoxFor(x=>x.Personal.FirstName)<br/>

    @Html.TextBoxFor(x=>x.Personal.Age)<br/>

    <input type="submit"/>
}
于 2012-11-08T15:38:05.233 に答える