質問

コンテキスト
私が持っているとしましょう:
Site.Masterレイアウト:

<div class="leftColumn">
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
<div class="rightColumn">
    <% Html.RenderPartial("_Login"); %>
    <asp:ContentPlaceHolder ID="SideContent" runat="server" />
</div>

ログインpartialViewは次のようになります:

<form action="/myApp/Account/Login" method="post">
    <input name="name" />Name<br />
    <input name="password" type="password" />Password<br />
    <button>Login</button>
</form>

コンテンツページ全体ではなく、ログインウィジェットフォームのみを更新することは可能ですか?

役に立ちましたか?

解決

http投稿を参照している場合、フォーム内の送信ボタンによって開始された(javascriptによって開始することもできる)投稿のみがサーバーに投稿されます。

フォームがネストされている場合、これは機能しません。外部フォームは常にサーバーに投稿されます。

以下のサンプルHTMLでは、最初のフォームの送信ボタンをクリックしても、2番目のフォームからサーバーに値が送信されません。同様に、2番目の送信ボタンをクリックしても、最初のフォームの値は投稿されません。

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

      <form action="/Login/Login" method="post">
        <input type="text" name="username" value="" />
        <input type="text" name="passowrd" value="" />
        <input type="submit" name="login" value="Login" />
      </form>


      <form action="/Login/AdminLogin" method="post">
        <input type="text" name="username" value="" />
        <input type="text" name="passowrd" value="" />
        <input type="submit" name="login" value="Login Admin" />
      </form>
    </div>
</body>
</html>

フォームセクションの1つだけを更新/変更したい場合は、javascriptを使用してjavascriptポスト(別名 Ajax )。

他のヒント

FormCollectionを受け入れるコントローラーメソッドを作成し、ビューに2つのフォームが定義されている場合、返されるformcollectionにはフォームAまたはフォームBの値が入力されます。formCollectionを検査し、値に基づいてロジックを分岐できますその中。非常に明確にしたい場合は、両方のフォームで同じ隠し変数を使用して、選択に役立つ値を設定できます。

これが1つのアプローチです。これに対処する方法はいくつかあります。

2つの単純なフォームがある場合、このアプローチを使用できます:

2つの異なる部分ビューを作成します。

@model CustomerInfoModel
@using (Ajax.BeginForm("CustomerInfo", "Customer", new AjaxOptions { HttpMethod = "Post", OnBegin = "InfoLoading", OnComplete = "InfoCompleted" }, new { id = "info", @class = "form-horizontal" }))
    {
    <input type="text" class="form-control" name="Name" id="Name" value="@Model.Name" />
    <input type="email" class="form-control" name="Email" id="Email"  value="@Model.Email" />
    <button type="submit" id="save-info" class="btn-medium red">Save</button>
    }

and

@model CustomerPasswordChangeModel
@using (Ajax.BeginForm("CustomerPasswordChange", "Customer", new AjaxOptions { HttpMethod = "Post", OnBegin = "InfoLoading", OnComplete = "InfoCompleted" }, new { id = "change", @class = "form-horizontal" }))
{
<input type="password" class="form-control" name="OldPassword" id="OldPassword"  value="" />
<input type="password" class="form-control" name="NewPassword" id="NewPassword"  value="" />
<button type="submit" id="save-change" class="btn-medium red" autocomplete="off">Save</button>
}

親ビューで、

@Html.Partial("CustomerInfo", Model.CustomerInfo)

and

@Html.Partial("CustomerPasswordChange", Model.CustomerPasswordChange)

コントローラー内:

    [HttpPost]
    public ActionResult CustomerInfo([Bind(Include = "Name,Email")] CustomerInfoModel model)
    {
        if (ModelState.IsValid)
            return new Json(new { success=true, message="Updated.", errors=null);

// do you logic

        return new Json(new { success=false, message="", errors=getHtmlContent(ModelState.Values.SelectMany(v => v.Errors).ToList(), "ModelError"));
    }

    [HttpPost]
    public ActionResult CustomerPasswordChange([Bind(Include = "OldPassword,NewPassword")] CustomerPasswordChangeModel model)
    {
        if (ModelState.IsValid)
            return new Json(new { success=true, message="Updated.", errors=null);

// do you logic

        return new Json(new { success=false, message="", errors=getHtmlContent(ModelState.Values.SelectMany(v => v.Errors).ToList(), "ModelError"));
    }

これはあなたがやりたいことをします。

注:getHtmlContentメソッドは、ページに表示されるエラーメッセージを生成するだけです。特別なことは何もありません。必要に応じて共有できます。

あなたの質問はあまり明確ではありません。

しかし、私が理解できる限り、答えはおそらく「はい」です。ユーザー入力に応じて、必要なものを更新できます。

if(pass != true) 
{ 
 ViewData["Message'] = "Hey your login failed!"; Return View("Login") 
}

ViewPageで

<form action="/tralala/Account/Login" method="post"> 
  <input name="name" />Name<br /> 
  <input name="password" type="password" />Password<br /> 

  <button>Login</button> 
 <div style="color: red"><%=ViewData["Message"] %><div> 
</form>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top