C# 在asp.net mvc中回发后保留文本框和下拉列表值

C# 在asp.net mvc中回发后保留文本框和下拉列表值,c#,asp.net-mvc,asp.net-mvc-4,postback,C#,Asp.net Mvc,Asp.net Mvc 4,Postback,如何在asp.net MVC中提交数据后获取旧值。我是MVC的新手。我阅读了许多文章以获得此结果,我的问题是在单击搜索按钮后,我丢失了文本框和下拉列表中的所有值 这是我的view.cshtml页面 @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { <input class="calendarr font" name="Arrival" value="@Request["Arrival"]" re

如何在asp.net MVC中提交数据后获取旧值。我是MVC的新手。我阅读了许多文章以获得此结果,我的问题是在单击搜索按钮后,我丢失了文本框和下拉列表中的所有值

这是我的view.cshtml页面

 @using (Html.BeginForm("Index", "Home", FormMethod.Post))
     {
       <input class="calendarr font" name="Arrival" value="@Request["Arrival"]" readonly type="text" id="Arrival"  onchange="changedDate()">

       <input class="calendarr font" name="Departure" value="@Request["Departure"]" readonly type="text" id="Departure" >

             <select id="Rooms" name="Rooms" class="dropdown">
                 @for (int i = 1; i <= Model.MaximumNumberOfRooms; i++)
                     {
                        <option value="@i @Request["Rooms"]">@i</option>
                            }
                        </select>


                 <select id="Persons" name="Persons" class="dropdown">
                            @for (int j = 1; j <= Model.MaximumNumberOfAdultsPerRoom; j++)
                            {
                                <option value="@j  @Request["Persons"]">@j</option>
                            }
                        </select>                          
                <select id="Childrens" name="Childrens" class="dropdown">
                            @for (int c = 1; c <= Model.MaximumNumberOfChildrenPerRoom; c++)
                            {
                                <option value="@c @Request["Childrens"]">@c</option>
                            }
                        </select>

              <input type="submit" class="btns" value="Search" />

     }
ASP.MVC中没有“回发”这样的东西。但是,这里有一个简单的方法:

  • 由于您发送的参数不敏感或数据库更改,因此将表单设置为FormMethod更合适。Get而不是FormMethod。Post:

    @using (Html.BeginForm("Index", "Home", FormMethod.Get))
    
  • 在索引_Get action中,按如下方式键入:

        public class HomeController : Controller
        {
            SessionHelper mysession = new SessionHelper();
    
    
            [HttpGet]
            [ActionName("Index")]
            public ActionResult Index_Get(string Arrival, string Departure, string Rooms, string Persons, string Childrens,  )
            {
                checkURL();
                pageload();
                ViewBag.Arrival = Arrival;
                ViewBag.Departure = Departure;
                ViewBag.Rooms = Rooms;
                ViewBag.Persons = Persons;
                ViewBag.Childrens = Childrens;
                return View(mysession);
            }
    }
    
  • 只要您的输入name作为参数存在于Index\u Get参数中,该参数将自动填充,无需调用请求对象。您还可以将参数类型从string更改为任意数据类型。但是,请注意,除非您将其他数据类型定义为可空值,否则它们不会接受空值

  • 在html中,将“每个字段”值更改为ViewBag对象,例如:

    value=“@ViewBag.Arrival”


  • 希望这有帮助:)

    将模型传递给视图,并使用强类型的
    HtmlHelper
    方法绑定到视图,然后发回您的model@StephenMuecke我的模型是**mysession**如何将其传递给视图,示例请访问MVC站点,学习基本教程。您获得了所有帮助,具有HtmlHelper的强类型视图,以及来自post action方法的post back模型。就这样!您不应该期望完整的代码。