单击提交按钮后,自动完成文本框在MVC3中不应为空

时间:2012-04-11 09:56:58

标签: asp.net-mvc

按钮点击后保留的mvc3文本框。 我的观点代码。

<div style="float: left">

States Filter :

</div>

<div style="float: left; padding-left: 10px">
   @Html.TextBox("Statestxt")
</div>

<div style="padding-left: 10px; float: left">
   <input type="image" value="submit" src="../../Images/FilterBrowse.gif" 
          alt="submit Button" />    
</div>

我的控制器:

public ActionResult AutocompleteAsync(string term)
{  
    var suggestions = from s in Adm.states
    select s.state_name;
    var namelist = suggestions.Where(n => n.ToLower().StartsWith(term.ToLower()));

    return Json(namelist, JsonRequestBehavior.AllowGet);
}



[AcceptVerbs(HttpVerbs.Post)]   
public ActionResult States(state stateModel, string _stateName, 
    FormCollection formvalues)
{  
   AdmDataContext Adm  = new AdmDataContext;

   if (Request.Form["Statestxt"] == null)
   {
      ViewBag.Error = "Enter State Name.";   
      ViewData["name"] = false;

      return View();
    }
    else
    {
        _stateName = Request.Form["Statestxt"].ToString();
        var record = (from state in Adm.states
                      where state.state_name == _stateName
                      select state).Count();

        if (record == 0)
        {
           ViewBag.Error = "Enter Valid State Name.";

           return View();

        }

        var _Stateid = from state in Adm.states
                       where state.state_name == _stateName
                       select state;

        int StateId = (int)_Stateid.First().state_id;
        var state1 = am.FindUpcomingStates2(StateId).ToList();

        if (state1 != null)
        {
            ViewData["name"] = true;
            return View("States", state1);
        }

     }

}

此处点击提交按钮后,文本框应为空。如何为文本框编写代码而不是空。

感谢。

1 个答案:

答案 0 :(得分:0)

为什么不使用View Model:

public class StateViewModel{

public string StateTxt { get; set; }

}

然后在视图(剃刀视图)

@model StateViewModel

@html.TextBox("StateTxt", Model.StateTxt)

然后在控制器上

[HttpPost]
public ActionResult States(StateViewModel model)
{
//Do here what you want

//Then put the text box empty

model.StateTxt =string.empty;

return View(model);

}

希望这有帮助