csharp Ajax.BeginForm

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Ajax.BeginForm相关的知识,希望对你有一定的参考价值。

@using WebApplication1.Models;
@model IEnumerable<RestaurantViewModel>

<div id="restaurantList">
    @foreach (var item in Model)
    {
        <div class="well-sm ">
            <h4>@item.Name</h4>
            <div>@item.City, @item.Country</div>
            <p>Reviews:@item.NumReviews</p>
            <hr />
        </div>
    }
</div>
@using WebApplication1.Models;
@model IEnumerable<RestaurantViewModel>


@using (Ajax.BeginForm(new AjaxOptions() {
    HttpMethod ="Get",
    InsertionMode=InsertionMode.Replace,
    UpdateTargetId ="restaurantList"}))
{
    <input type="text" name="search" value="" />
    <input type="submit" value="Search by name" />
}



@Html.Partial("_Restaurants",Model)
public ActionResult Index(string search="")
{
    var model = db.Restaurants
            .Where(r => r.Name.Contains(search))
            .OrderByDescending(r => r.Reviews.Average(rr => rr.Rating))
            .Select(r => new RestaurantViewModel { ID = r.ID, Name = r.Name, Country = r.Country, City = r.City, NumReviews = r.Reviews.Count })
            .Take(10);
    if (Request.IsAjaxRequest())
    {
        return PartialView("_Restaurants",model);
    }
    else
    {
        return View(model);
    }
}

以上是关于csharp Ajax.BeginForm的主要内容,如果未能解决你的问题,请参考以下文章

ajax.beginform - OnFailure 不返回部分视图

为啥 Ajax.BeginForm 不存在?

form表单和ajax表单提交(Html.BeginForm()Ajax.BeginForm())的差别

通过 ActionLink 提交 Ajax.BeginForm

MVC Ajax.BeginForm 替换奇怪的行为

Ajax.BeginForm 验证模型