将数据返回到局部视图

将数据返回到局部视图

本文介绍了将数据返回到局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与它的局部视图,使用户可以报名参加我的电子邮件新闻复制一个侧边栏一个博客。我正在试图做的是用户返回到它们在窗体的局部视图发布一些数据,并显示任何确认或返回的消息后,来到该页面。

的问题是,我的局部视图在新窗口中打开(不布局设计)。我怎样才能解决这一问题,以便它返回到我的博客,在德侧边栏返回的数据?

这是我的看法:

  @using Blog.Models.Entities
@model用户<报头GT;
    < H2>&订阅LT; / H>
< /头>< P>订阅我的电子邮件新闻传播与LT; / P>@using(Html.BeginForm(形,认购))
{
    < D​​IV CLASS =编辑标记> @ Html.LabelFor(用户=> subscriber.Email)LT; / DIV>
    < D​​IV CLASS =主编场> @ Html.EditorFor(用户=> subscriber.Email)LT; / DIV>    @ Html.ValidationMessageFor(用户=> subscriber.Email)    <输入类型=提交值=订阅/>    &所述p为H.; @ ViewBag.Result&下; / P>
}

和控制器的相关件正在处理中的数据:

 公众的ActionResult表()
{
    返回PartialView(_表);
}[HttpPost]
公众的ActionResult表(用户订户)
{
    如果(ModelState.IsValid)
    {
        订户foundSubscriber = _repository.Subscribers.Where(S => s.Email.Equals(subscriber.Email))。FirstOrDefault();
        如果(foundSubscriber!= NULL)
        {
            ModelState.AddModelError(电子邮件,该E-mail地址已经被添加。);
            返回PartialView(_表,用户);
        }        _repository.SaveSubscriber(用户);        ViewBag.Result =成功与订阅电子报。
        返回PartialView(_表);
    }    ModelState.AddModelError(电子邮件,请提供一个有效的E-mail地址。);
    返回PartialView(_表,用户);
}


解决方案

我终于找到了解决问题的办法。我用AJAX实现它,并结束了与以下code:

_Index.cshtml

 <报头GT;
    < H2>&订阅LT; / H>
< /头>< P>订阅我的电子邮件新闻传播与LT; / P>< D​​IV ID =订阅的形式>
    @ {Html.RenderPartial(_表);}
< / DIV>

_Form.cshtml

  @using Blog.Models.Entities
@model用户
@ {
    AjaxOptions ajaxOptions =新AjaxOptions
    {
        LoadingElementId =加载,
        LoadingElementDuration = 2000,
        列举HTTPMethod =邮报,
        UpdateTargetId =订阅的形式
    };
}< D​​IV ID =装的风格=显示:无;>
    < P>处理的请求......< / P>
< / DIV>@using(Ajax.BeginForm(指数,订阅,ajaxOptions))
{
    < D​​IV CLASS =编辑标记> @ Html.LabelFor(用户=> subscriber.Email)LT; / DIV>
    < D​​IV CLASS =主编场> @ Html.EditorFor(用户=> subscriber.Email)LT; / DIV>    @ Html.ValidationMessageFor(用户=> subscriber.Email)    <输入类型=提交值=订阅/>
}

_Succes.cshtml

  @using Blog.Models.Entities
@model用户< p n =订阅的结果> @ ViewBag.Result< / P>

和下面的控制器操作方法:

 公众的ActionResult指数()
{
    返回PartialView(_指数);
}[HttpPost]
公共PartialViewResult指数(用户订户)
{
    如果(ModelState.IsValid)
    {
        订户foundSubscriber = _repository.Subscribers.Where(S => s.Email.Equals(subscriber.Email))。FirstOrDefault();
        如果(foundSubscriber!= NULL)
        {
            ModelState.AddModelError(电子邮件,该E-mail地址已经被添加。);
            返回PartialView(_表,用户);
        }        _repository.SaveSubscriber(用户);        ViewBag.Result =成功与订阅电子报。
        返回PartialView(_更迭,用户);
    }    ModelState.AddModelError(电子邮件,请提供一个有效的E-mail地址。);
    返回PartialView(_表,用户);
}

我希望这将有助于任何人试图在未来实现相同。顺便说一句,我发现在这个博客上的解决方案:的.

I have a blog that has a sidebar with a partial view in it that enables users to sign up for my e-mail newsfeed. What I'm trying to do is returning the user to the page they came from after posting some data, and displaying any validation or return messages in the form's partial view.

The problem is that my partial view opens in a new window (without the lay-out). How can I fix this so it returns to my blog, with the return data in de sidebar?

This is my view:

@using Blog.Models.Entities
@model Subscriber

<header>
    <h2>Subscribe</h2>
</header>

<p>Subscribe to my e-mail newsfeed.</p>

@using (Html.BeginForm("Form", "Subscription"))
{
    <div class="editor-label">@Html.LabelFor(subscriber => subscriber.Email)</div>
    <div class="editor-field ">@Html.EditorFor(subscriber => subscriber.Email)</div>

    @Html.ValidationMessageFor(subscriber => subscriber.Email)

    <input type="submit" value="Subscribe" />

    <p>@ViewBag.Result</p>
}

And the relevant pieces of controller that are processing the data:

public ActionResult Form()
{
    return PartialView("_Form");
}

[HttpPost]
public ActionResult Form(Subscriber subscriber)
{
    if (ModelState.IsValid)
    {
        Subscriber foundSubscriber = _repository.Subscribers.Where(s => s.Email.Equals(subscriber.Email)).FirstOrDefault();
        if (foundSubscriber != null)
        {
            ModelState.AddModelError("Email", "This e-mail address has already been added.");
            return PartialView("_Form", subscriber);
        }

        _repository.SaveSubscriber(subscriber);

        ViewBag.Result = "Succesfully subscribed to the newsletter.";
        return PartialView("_Form");
    }

    ModelState.AddModelError("Email", "Please provide a valid e-mail address.");
    return PartialView("_Form", subscriber);
}
解决方案

I finally found the solution to the problem. I implemented it with AJAX and ended up with the following code:

_Index.cshtml

<header>
    <h2>Subscribe</h2>
</header>

<p>Subscribe to my e-mail newsfeed.</p>

<div id="subscription-form">
    @{Html.RenderPartial("_Form");}
</div>

_Form.cshtml

@using Blog.Models.Entities
@model Subscriber
@{
    AjaxOptions ajaxOptions = new AjaxOptions
    {
        LoadingElementId = "loading",
        LoadingElementDuration = 2000,
        HttpMethod = "Post",
        UpdateTargetId = "subscription-form"
    };
}

<div id="loading" style="display: none;">
    <p>Processing request...</p>
</div>

@using (Ajax.BeginForm("Index", "Subscription", ajaxOptions))
{
    <div class="editor-label">@Html.LabelFor(subscriber => subscriber.Email)</div>
    <div class="editor-field ">@Html.EditorFor(subscriber => subscriber.Email)</div>

    @Html.ValidationMessageFor(subscriber => subscriber.Email)

    <input type="submit" value="Subscribe" />
}

_Succes.cshtml

@using Blog.Models.Entities
@model Subscriber

<p id="subscription-result">@ViewBag.Result</p>

And the following controller action methods:

public ActionResult Index()
{
    return PartialView("_Index");
}

[HttpPost]
public PartialViewResult Index(Subscriber subscriber)
{
    if (ModelState.IsValid)
    {
        Subscriber foundSubscriber = _repository.Subscribers.Where(s => s.Email.Equals(subscriber.Email)).FirstOrDefault();
        if (foundSubscriber != null)
        {
            ModelState.AddModelError("Email", "This e-mail address has already been added.");
            return PartialView("_Form", subscriber);
        }

        _repository.SaveSubscriber(subscriber);

        ViewBag.Result = "Succesfully subscribed to the newsletter.";
        return PartialView("_Succes", subscriber);
    }

    ModelState.AddModelError("Email", "Please provide a valid e-mail address.");
    return PartialView("_Form", subscriber);
}

I hope this will help anyone trying to achieve the same in the future. BTW, I found the solution on this blog: http://xhalent.wordpress.com/2011/02/05/using-unobtrusive-ajax-forms-in-asp-net-mvc3/.

这篇关于将数据返回到局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:03