MVC textbox postback issue

I have a text box users can enter search criteria. I also have a check box to filter out records. I want them to be able to search on the check box and not have to enter anything in the text box. Every time I leave the text box empty and hit Search, it return the focus to the text box and won't hit the controller.

@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>                          
}    

Model:

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

Controller:

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

Any help would be appreciated.


Every time I leave the text box empty and hit Search, it return the focus to the text box and won't hit the controller.

Your SearchFor property is required on Model:

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

so it is not surprising that if you leave it empty validation fails. If you have enabled client side validation (by including the jquery.validate and jquery.validate-unobtrusive scripts) the controller won't even be hit because validation will occur on the client.

If you don't want this property to be required you should remove the Required attribute from it.

链接地址: http://www.djcxy.com/p/53960.html

上一篇: C#MVC4更改视图属性不改变视图HTML页面上的数据

下一篇: MVC文本框回发问题