0

我有一个文本框,用户可以输入搜索条件。我还有一个复选框来过滤掉记录。我希望他们能够在复选框上进行搜索,而不必在文本框中输入任何内容。每次我将文本框留空并点击搜索时,它都会将焦点返回到文本框并且不会点击控制器。

@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, "必须提供参数");

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

任何帮助,将不胜感激。

4

1 回答 1

0

每次我将文本框留空并点击搜索时,它都会将焦点返回到文本框并且不会点击控制器。

SearchFor模型需要您的属性:

[Required(ErrorMessage="Please enter a search string before submitting the form.")]
public string SearchFor { get; set; }

因此,如果您将其留空,验证失败也就不足为奇了。如果您启用了客户端验证(通过包含jquery.validateandjquery.validate-unobtrusive脚本),控制器甚至不会被命中,因为验证将发生在客户端上。

如果您不希望需要此属性,则应Required从中删除该属性。

于 2012-08-14T21:05:22.243 回答