2017-04-18 83 views
-1

如何在asp.net中提交数据后获取旧值MVC.i是MVC的新东西我读了很多文章以达到这个结果,我的问题是点击搜索按钮后,我失去了文本框中的所有值和下拉列表。在asp.net中回发后保留文本框和下拉列表的值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" /> 

    } 

而且在HomeController的下面的代码。

public class HomeController : Controller 
 
    { 
 
     SessionHelper mysession = new SessionHelper(); 
 
     
 
     [HttpPost] 
 
     [ActionName("Index")] 
 
     public ActionResult Index_Post() 
 
     {            
 
      return View(); 
 
     } 
 

 
     [HttpGet] 
 
     [ActionName("Index")] 
 
     public ActionResult Index_Get() 
 
     { 
 
      checkURL(); 
 
      pageload(); 
 
      string arrival = Request.Form["Arrival"]; 
 
      string Departure = Request.Form["Departure"]; 
 
      string rooms = Request.Form["Rooms"]; 
 
      string Persons = Request.Form["Persons"]; 
 
      string Childrens = Request.Form["Childrens"]; 
 
      return View(mysession); 
 
     } 
 
}

感谢。

+0

通行证一个模型的视图,并绑定到您的视图使用强类型的'HtmlHelper'方法和回发您的模型 –

+0

@StephenMuecke我的模型是** mysession **我怎么能把它传递给视图,请举例 – ahosam

+2

您需要转到MVC网站并通过教程学习基础知识。 –

回答

0

在ASP.MVC中没有“PostBack”这样的事情。然而,在这里一个简单的方法来做到这一点:

  1. 由于您发送的参数不敏感或数据库的变化,它更适合有你的形式FormMethod.Get代替FormMethod.Post:

    @using (Html.BeginForm("Index", "Home", FormMethod.Get)) 
    
  2. 在你Index_Get行动,喜欢这类型的:只要

    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); 
         } 
    } 
    

为您输入名称作为Index_Get参数内的参数存在,该参数将被自动填充而不需要调用Request对象。您还可以将参数类型从字符串更改为任何数据类型。但是,请注意其他数据类型不会接受空值,除非将它们定义为可空。

  • 在HTML中每个字段的值更改为ViewBag对象,如:

    value="@ViewBag.Arrival"

  • 希望这有助于:)

    相关问题