简体   繁体   English

Telerik MVC Tabstrip表单提交失去焦点并在发布后离开页面

[英]Telerik MVC Tabstrip form submit loses focus and leaves page after Post

I'm working with Telerik controls in an MVC 4 project. 我正在MVC 4项目中使用Telerik控件。 Specifically, I have a page that has a parent/child grid setup. 具体来说,我有一个具有父/子网格设置的页面。 The child grid is in a Tabstrip and I have another Tab where I would like to allow a user to submit changes for their contact information. 子网格位于一个Tabstrip中,而我还有另一个Tab,我希望允许用户提交其联系信息的更改。

The example/demo I used from Telerik can be found here - http://demos.telerik.com/aspnet-mvc/razor/grid/detailsajax . 我在Telerik中使用的示例/演示可在此处找到-http: //demos.telerik.com/aspnet-mvc/razor/grid/detailsajax

At this point, I was able to setup my code to have a partial view and bring up the form successfully in the tab. 至此,我可以将代码设置为具有部分视图,并在选项卡中成功显示表单。 I had one test field for email that I was able to retrieve and populate against the model. 我有一个用于电子邮件的测试字段,可以根据该模型进行检索和填充。 Validation is working. 验证有效。 My problem now is that, after the form submits, it is leaving child tab and bringing up a new page based on the partial view instead of staying within the tab its already on. 我的问题现在是,提交表单后,它将离开子选项卡,并基于部分视图弹出一个新页面,而不是停留在已经打开的选项卡中。

So, how can I keep the focus within the current child tab after the form has been submitted? 那么,提交表单后如何将焦点保持在当前子选项卡中? I'm fairly new to MVC in general so it maybe something not even specific to Telerik. 总的来说,我对MVC还是很陌生,所以也许它甚至不是Telerik特有的。 Thanks in advance for any advice and here's some code snippets below. 在此先感谢您的任何建议,下面是一些代码片段。

View code from my main page. 从我的主页查看代码。

.Items(items =>
                {
                    items.Add().Text("CourseHistory").Content(
                            Html.Telerik().Grid<selStudentCourseHistory_Result>()
                                ...grid code snipped...
                                ...
                    items.Add().Text("Contact Information")
                        .LoadContentFrom("StudentSubmitContactInfo", "Student", new { studentid = "<#=StudentId #>" })

                                .Visible(true);
                             })

                       .ToHtmlString()

Partial View for StudentSubmitContactInfo StudentSubmitContactInfo的局部视图

@using (Html.BeginForm())
{
    <div id="dvStudentContact">
        <table>
            <tr>
                <td><label>SC Email:</label></td>
                <td>@Html.TextBoxFor(s => s.scEmail)</td>
                <td></td>
                <td></td>
                <td>
                    <input type="submit" name="btnContactSubmit" value="Submit" id="btnContactSubmit"/>
                </td>
            </tr>
        </table>
    </div>

    @Html.ValidationSummary()
}

Code From the Controller 来自控制器的代码

public ActionResult StudentSubmitContactInfo(int studentid)
    {

        StudentContactModel contactinfo = (from s in new LNLiteEntities().Students
                                                       where s.StudentId == studentid
                                                       select new StudentContactModel
                                                       {
                                                           scEmail = s.Email
                                                       }).FirstOrDefault();

        return PartialView(contactinfo);
    }

    [HttpPost]
    public ActionResult StudentSubmitContactInfo(StudentContactModel scm)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("StudentSubmitContactInfo");
        }
        else {
            return PartialView(scm);
        }

    }

As it sometimes happens, I was able to finally come with a different search criteria to get this resolved. 由于有时会发生这种情况,因此我终于能够使用其他搜索条件来解决此问题。 Here is what I did. 这是我所做的。

For the Controller, HttpPost portion was changed to return Json. 对于Controller,HttpPost部分已更改为返回Json。

public ActionResult StudentSubmitContactInfo(int studentid)
    {

        StudentContactModel contactinfo = (from s in new LNLiteEntities().LNStudents
                                                       where s.StudentId == studentid
                                                       select new StudentContactModel
                                                       {
                                                           scEmail = s.Email
                                                       }).FirstOrDefault();

        return PartialView(contactinfo);
    }

    [HttpPost]
    public JsonResult StudentSubmitContactInfo(StudentContactModel scm)
    {

        if (ModelState.IsValid)
        {
            return Json(new { contactinfo = scm, errorMsg = "success", Success = true});
        }
        else
        {
            return Json(new { contactinfo = scm, errorMsg = "invalid values", Success = false });

        }

    }

So, it no longer went off to some other page which is great. 因此,它不再跳转到其他很棒的页面。 However, validation stopped working. 但是,验证停止工作。 So, the fix for that was in the jquery submit in the partial view. 因此,此问题的解决方法是在局部视图的jquery提交中。 Make sure you have the partial view setup for jquery.validate and jquery.validate.unobtrusive. 确保您具有jquery.validate和jquery.validate.unobtrusive的部分视图设置。

@model StudentContactModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript">  </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm("StudentSubmitContactInfo", "Student", FormMethod.Post, new { id = "contact-form" }))
{
    <div id="dvStudentContact">
        <table>
            <tr>
                <td><label>SC Email:</label></td>
                <td>@Html.TextBoxFor(s => s.scEmail)</td>
                <td></td>
                @*<td>@Html.TextBoxFor(s => s.scEmail, new {@Value = "<#=WorkEmail #>" })</td>*@
                <td>
                    <input type="submit" name="btnContactSubmit" value="Submit" id="btnContactSubmit"/>
                </td>
            </tr>
        </table>
    </div>

    @Html.ValidationSummary()
}

<script type="text/javascript">
$('#contact-form').submit(function (e) {
    var data = $(this).serialize();
    $.post(this.action, data, function (data) {
        //prevents the window from closing
        //e.preventDefault();

        if (data.Success == true) {
            alert(data.contactinfo.scEmail);
        }
        else {
            alert(data.Success);
        }

    });
    //e.preventDefault();
    var form = $("#contact-form").closest("form");
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse($("#dvStudentContact"));

    e.preventDefault();
});

Use javascript code to set the focus on that tab by following sample code 通过以下示例代码,使用javascript代码设置对该标签的关注

            var tabstrip = $("#controlID").data("tTabStrip");
            var item = $("li", tabstrip.element)["6"];
            tabstrip.select(item);

Refer to these links for more details http://demos.telerik.com/aspnet-mvc/tabstrip/clientsideapi http://www.telerik.com/community/forums/aspnet-mvc/tabstrip/select-is-not-functioning.aspx 有关更多详细信息,请参考这些链接http://demos.telerik.com/aspnet-mvc/tabstrip/clientsideapi http://www.telerik.com/community/forums/aspnet-mvc/tabstrip/select-is-not- function.aspx

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

相关问题 Telerik MVC标签选择标签,显示验证错误 - Telerik MVC tabstrip select tab with validation error Telerik MVC-无法动态加载Tabstrip内容 - Telerik MVC - Not Loading Tabstrip Content Dynamically 如何以编程方式将项目添加到Telerik MVC TabStrip? - How to add items programatically to the Telerik MVC TabStrip? 如何以编程方式选择Telerik MVC TabStrip? - How to select the Telerik MVC TabStrip programatically? 为什么我必须刷新页面才能使Telerik TabStrip在ASP.Net MVC 4中更新? - Why do I have to do a page refresh to get my Telerik TabStrip to update in ASP.Net MVC 4? ASP.NET 带 EF 页面的 MVC 在通过 return View() 发布后丢失 model 数据 - ASP.NET MVC with EF page loses model data after post via return View() 在Telerik TabStrip(MVC)中,如何获取选定的标签索引? - In Telerik TabStrip (MVC), how to get the selected tab index? 在Telerik MVC扩展中的Tabstrip项的URL中添加“目标”属性 - Adding a 'target' attribute to the URL of a tabstrip item in Telerik MVC extensions 在Telerik Tabstrip中使用时,不会渲染form元素 - The form element is not rendered when used inside Telerik Tabstrip 如果使用自定义弹出编辑器,Telerik MVC Grid 在插入或取消后会留下空字段 - Telerik MVC Grid leaves empty fields after insert or cancel if custom popup editor is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM