简体   繁体   English

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

[英]Ajax query refreshing whole page instead of part

I have simple View where I have a ajax form I use for filtering records: 我有一个简单的View,其中有一个我用来过滤记录的ajax表单:

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

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

   @Html.Partial("SearchTab") 
}

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

Partial SearchTab: 部分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 />

Controller action: 控制器动作:

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

After writing something into input field and pressing filter. 在将内容写入输入字段并按过滤器之后。 Ajax should refresh ShowPartial and keep values in the input fields from SerchTab but instead I get filtered records and inputs are getting empty. Ajax应该刷新ShowPartial并将值保留在SerchTab的输入字段中,但相反,我得到过滤的记录,并且输入内容为空。 Can anyone suggest me edits to change this behaviour 谁能建议我编辑以更改此行为

我不知道这是否有帮助,因为它正在使用jquery,但它的工作方式如您所愿: http : //code.tutsplus.com/tutorials/submit-a-form-without-page-refresh-using-jquery- -net-59

If you are intending to 如果您打算

return View()

you should also return the original model so that control values can be populated with your captured values. 您还应该返回原始模型,以便可以使用捕获的值填充控制值。

Alternatively if you just need to return the list: 或者,如果您只需要返回列表:

deviceusages.ToList()

Then you can return a PartialView() eg; 然后,您可以返回PartialView()例如;

return PartialView(deviceusages.ToList());

Ok I found cause of a problem. 好的,我发现了问题的原因。

I need to change this code 我需要更改此代码

<div class="input-group">
    <div class="input-group-addon name">
        User name:
    </div>
    <input type="text" name="name" class="form-control" />
</div>

into same thing made wit use of .net Html helpers. 使用.net HTML帮手来处理相同的事情。 After this change everything works! 更改之后,一切正常!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM