MVC文本框回发问题
我有一个文本框用户可以输入搜索条件。 我也有一个复选框来过滤记录。 我希望他们能够在复选框上进行搜索,而不必在文本框中输入任何内容。 每次我将文本框留空并点击搜索时,它会将焦点返回到文本框,并且不会击中控制器。
@using (Html.BeginForm("Index", "Ebooks")) {
<div class="editor-label">
Enter Author, ISBN, Title or Imprint
</div>
<div class="editor-field">
<p>Enter author, isbn, title or imprint. Use commas to seperate criteria.</p>
@Html.TextBoxFor(model => model.SearchFor)
<br />
@Html.LabelFor(model => model.deniedBook)
@Html.CheckBoxFor(model => model.deniedBook)
<input type="submit" value="Search" />
</div>
}
模型:
公共类ItemListViewModel {
public ItemListViewModel() {
this.Page = 1;
this.StartPosition = 0;
this.EndPosition = 0;
this.TotalResults = 0;
this.TotalPages = 0;
this.SearchFor = string.Empty;
this.deniedBook = false;
}
public ItemListViewModel(int count, int pageSize, int page, string searchFor) {
int startPos = (page == 1) ? 1 : ((page - 1) * pageSize) + 1;
decimal totalPages = (decimal)count / (decimal)pageSize;
this.StartPosition = startPos;
this.EndPosition = ((startPos + pageSize) > count) ? count : (startPos - 1) + pageSize;
this.TotalResults = count;
this.Page = page;
this.TotalPages = (int) Math.Ceiling(totalPages);
this.SearchFor = searchFor;
}
public ItemListViewModel(int count, int pageSize, int page, string searchFor, bool deniedBooks)
{
int startPos = (page == 1) ? 1 : ((page - 1) * pageSize) + 1;
decimal totalPages = (decimal)count / (decimal)pageSize;
this.StartPosition = startPos;
this.EndPosition = ((startPos + pageSize) > count) ? count : (startPos - 1) + pageSize;
this.TotalResults = count;
this.Page = page;
this.TotalPages = (int)Math.Ceiling(totalPages);
this.SearchFor = searchFor;
this.deniedBook = deniedBooks;
}
public int StartPosition { get; set; }
public int EndPosition { get; set; }
public int TotalResults { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
[Required(ErrorMessage="Please enter a search string before submitting the form.")]
public string SearchFor { get; set; }
public IEnumerable<Object> Objects { get; set; }
public IEnumerable<ObjectImage> ObjectImages { get; set; }
public bool deniedBook { get; set; }
}
控制器:
[HttpPost] public ActionResult Index(ItemListViewModel model){if(model == null)throw new ArgumentOutOfRangeException(“model”,model,“Arguments must be provided”);
// This action will cache the list if needed then pass the model through the querystring to the search Action
// Using this method to allow for "back button" compatibility and direct links to searches and pages
model = PrepareResults(model);
// Make sure we have records to return
if (model.TotalResults > 0)
{
model = LoadResultSet(model);
return View("Index", model);
}
// If we're still here there were no records
ViewBag.Message = "No eBooks were found that match the search criteria '" + model.SearchFor + "'.";
return View("Index", model);
}
任何帮助,将不胜感激。
每次我将文本框留空并点击搜索时,它会将焦点返回到文本框,并且不会击中控制器。
模型上需要您的SearchFor
属性:
[Required(ErrorMessage="Please enter a search string before submitting the form.")]
public string SearchFor { get; set; }
所以如果你放弃验证失败就不足为奇了。 如果您启用了客户端验证(通过包含jquery.validate
和jquery.validate-unobtrusive
脚本),控制器甚至不会被击中,因为客户端上会进行验证。
如果你不想要这个属性是必需的,你应该从它中移除Required
属性。