我有一个文本框,用户可以输入搜索条件。我还有一个复选框来过滤掉记录。我希望他们能够在复选框上搜索,而不必在文本框中输入任何内容。每次我将文本框留空并点击“搜索”时,它会将焦点返回到文本框,并且不会点击控制器。
@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)抛出新的ArgumentOutOfRangeException(&#34; model&#34;,model,&#34;必须提供参数&#34;);
// 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);
}
任何帮助将不胜感激。
答案 0 :(得分:0)
每当我将文本框留空并点击“搜索”时,它就会返回 专注于文本框,不会碰到控制器。
Model:
上需要SearchFor
属性
[Required(ErrorMessage="Please enter a search string before submitting the form.")]
public string SearchFor { get; set; }
因此,如果您将其留空,则验证失败也就不足为奇了。如果您已启用客户端验证(通过包含jquery.validate
和jquery.validate-unobtrusive
脚本),控制器甚至不会被命中,因为验证将在客户端上进行。
如果您不想要此属性,则应从中删除Required
属性。