Ajax查询刷新整个页面而不是部分

时间:2014-02-10 09:30:31

标签: c# ajax asp.net-mvc

我有一个简单的View,我有一个用于过滤记录的ajax表单:

@using (Ajax.BeginForm("Index", new AjaxOptions()
{

    InsertionMode=InsertionMode.Replace,
    UpdateTargetId="dane"
}))
{

   @Html.Partial("SearchTab") 
}

@Html.Partial("ShowPartial") // <--- id="dane"

部分SearchTab:

<div class="row">
        <div class="col-lg-3">
            <div class="input-group">
                <div class="input-group-addon name">
                    User name:
                </div>
                <input type="text" name="name" class="form-control" />
            </div>
        </div>
        <div class="col-lg-3">
            <div class="input-group">
                <div class="input-group-addon surname">
                    User surname:
                </div>
                <input type="text" name="surname" class="form-control" />
            </div>
        </div>
        <div class="col-lg-3">
            <div class="input-group">
                <div class="input-group-addon devicename">
                    Device name:
                </div>
                <input type="text" name="deviceName" class="form-control" />
            </div>
        </div>
        <div class="col-lg-3">
            <div class="input-group">
                <div class="input-group-addon devicemanufacturer">
                    Device Manufactuer:
                </div>
                <input type="text" name="deviceManufacturer" class="form-control" />
            </div>
        </div>
    </div>
    <input type="submit" class="btn btn-default" value="Filter" id="filter"/>
    <br />

控制器操作:

public ActionResult Index(string name, string surname ,string deviceName, string deviceManufacturer, string Page)
        {
            bool RoleId = ad.CheckIfAdmin(Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@"\") + 1));
            ViewBag.RoleId = RoleId;
            var deviceusages = db.DeviceUsages.Include(d => d.DeviceInstance).Include(d => d.Storage).Include(d => d.User).Where(w=>w.UserId!=6).Skip((int.Parse(Page)-1)*30).Take(30);
            if(name!="" && name!=null)
            {
                deviceusages = deviceusages.Where(w => w.User.Name.Contains(name));
            }
            if (surname != "" && surname != null)
            {
                deviceusages = deviceusages.Where(w => w.User.Surname.Contains(surname));
            }
            if (deviceName != "" && deviceName != null)
            {
                deviceusages = deviceusages.Where(w => w.DeviceInstance.Device.Name.Contains(deviceName));
            }
            if (deviceManufacturer!= "" && deviceManufacturer != null)
            {
                deviceusages = deviceusages.Where(w => w.DeviceInstance.Device.Manufacturer.Contains(deviceManufacturer));
            }
            return View(deviceusages.ToList());
        }

将某些内容写入输入字段并按下过滤器后。 Ajax应刷新ShowPartial并在SerchTab的输入字段中保留值,但我得到过滤记录并且输入变空。任何人都可以建议我进行编辑以改变这种行为

3 个答案:

答案 0 :(得分:0)

我不知道这是否有用,因为它正在使用jquery,但它的工作方式与你想要的一样: http://code.tutsplus.com/tutorials/submit-a-form-without-page-refresh-using-jquery--net-59

答案 1 :(得分:0)

如果您打算

return View()

您还应该返回原始模型,以便可以使用捕获的值填充控制值。

或者,如果您只需要返回列表:

deviceusages.ToList()

然后你可以返回一个PartialView()例如。

return PartialView(deviceusages.ToList());

答案 2 :(得分:0)

好的,我找到了问题的原因。

我需要更改此代码

<div class="input-group">
    <div class="input-group-addon name">
        User name:
    </div>
    <input type="text" name="name" class="form-control" />
</div>
使用.net Html助手做同样的事情。 在这一改变之后一切正常!

@Html.Label("User name: ", new { @class = "input-group-addon" })
@Html.TextBox("name", null, new { @type = "text", @class = "form-control" })