ユーザーが検索条件を入力できるテキスト ボックスがあります。レコードを除外するためのチェック ボックスもあります。チェックボックスで検索できるようにして、テキストボックスに何も入力する必要がないようにしたい. テキスト ボックスを空のままにして [検索] を押すたびに、フォーカスがテキスト ボックスに戻り、コントローラーにヒットしません。
@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>
}
モデル:
public class 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);
}
どんな助けでも大歓迎です。