0

私はmvcが初めてです。私は小さなフォームしか持っていません。フォームを送信すると、フォームを配置したのと同じ場所に部分的なビューがレンダリングされるようにしたいと考えています。

ここで、私たちのページがどのように見えるかをスクリーンショットで示しています。 ここに画像の説明を入力

部分的なビューをレンダリングすると、ページが消えて裸のように表示されます。これが再びスクリーンショットです。 ここに画像の説明を入力

successfully saveフォームを配置したのと同じ場所にメッセージを表示したい。

これが私の完全なコードです。

私のコントローラーコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcPractise.Controllers
{
    public class GameController : Controller
    {
        //
        // GET: /Game/

        public ActionResult Test()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Save(string name, string salary, string btnSubmit)
        {
            //return View("Message");
            return PartialView("MyMessage");
        }

        public ActionResult Index()
        {
            return View();
        }
    }
}

フォームデータがある私のメインビューフォーム

@{
    ViewBag.Title = "Test";
}

<h2>Hello222</h2>

<div id="mydiv">
@using (Html.BeginForm("Save", "Game", FormMethod.Post, new { @Id = "Form1" }))
{
    <table border="0">
    <tr>
        <td>Name :</td>
        <td><input name="name" type="text" /></td>
    </tr>

     <tr>
        <td>Salary :</td>
        <td><input name="salary"  type="text" /></td>
    </tr>
    <tr>
        <td colspan="2"><input name="btnSubmit" type="submit" value="Save"/></td>
    </tr>
    </table>
}
</div>

私のパーシャルビューでは、このテキストしかありません

<h2>Successfully Saved</h2>

jqueryを使用せずに何をする必要があるかを教えてください。ありがとう

4

1 に答える 1

1

ViewBag または ViewData を使用する必要があります。データを正常に保存すると、同じビューとメッセージを返すことができます。

コントローラ

[HttpPost]
public ActionResult Save(string name, string salary, string btnSubmit)
{
  if(/*check if has success*/)
     ViewBag.Success = true;
  /*do another stuff*/
  return View("View where form data is there");
}

フォーム ビュー:

@{
    ViewBag.Title = "Test";
}

<h2>Hello222</h2>

      @if(ViewBag.Success != null && ViewBag.Success == true) //Show the message
      {
         <h2>Successfully Saved</h2>
      }

<div id="mydiv">
@using (Html.BeginForm("Save", "Game", FormMethod.Post, new { @Id = "Form1" }))
{
    <table border="0">
    <tr>
        <td>Name :</td>
        <td><input name="name" type="text" /></td>
    </tr>

     <tr>
        <td>Salary :</td>
        <td><input name="salary"  type="text" /></td>
    </tr>
    <tr>
        <td colspan="2"><input name="btnSubmit" type="submit" value="Save"/></td>
    </tr>
    </table>
}
</div>

編集:

Partial View を使用すると同じ動作を得ることができますが、コントローラーから戻るのではなく、PartialView をページに直接レンダリングします。PartialView は、ページ全体ではなく、HTML の一部をレンダリングすることを想定しています。これが PartialView の主な目的です。

PartialViewSuccess.cshtml:

<h2>Successfully Saved</h2>

次に、ページで PartialView をレンダリングします。

@if(ViewBag.Success != null && ViewBag.Success == true) //Show the message
{
  Html.RenderPartial("Success");
}
于 2013-09-09T15:10:30.643 に答える