【问题标题】:render partial view insted of other partial view渲染局部视图而不是其他局部视图
【发布时间】:2015-05-26 11:59:24
【问题描述】:

我有一个表单,它是布局页面中的部分视图。我需要在成功后首先呈现另一个局部视图。

但是,局部视图在第一个局部视图内呈现。 (例如,如果用户在 Mydomain/index 中填写表单,新的局部视图应该显示在相同的 url 中。现在它显示在 /controller/create 中)。

[ChildActionOnly]
public ActionResult Create()
{
    ViewBag.SubjectId = new SelectList(db.subjects, "SubjectId", "Subjectname");
    return PartialView();
}

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "InfoId,FirstName,SureName,PhoneNumber,Address,Email,EnterdDate,IsActive,Tipol,SubjectId")] Info info)
    {

        info.EnterdDate = DateTime.Now;
        info.IsActive = false;
        if (ModelState.IsValid)
        {
            db.infos.Add(info);
            db.SaveChanges();
            TempData["thanks"] = info.FullName;
            return PartialView("_bla");
        }

        ViewBag.SubjectId = new SelectList(db.subjects, "SubjectId", "Subjectname", info.SubjectId);
        return View(info);
    }


    public PartialViewResult _bla()
    {
        var thanks = TempData["thanks"];
        return PartialView();
    }

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "InfoId,FirstName,SureName,PhoneNumber,Address,Email,EnterdDate,IsActive,Tipol,SubjectId")] Info info)
    {
    
            info.EnterdDate = DateTime.Now;
            info.IsActive = false;
            if (ModelState.IsValid)
            {
                db.infos.Add(info);
                db.SaveChanges();
                TempData["thanks"] = info.FullName;
                return RedirectToAction("Create");
            }
    
          ViewBag.SubjectId = new SelectList(db.subjects, "SubjectId", "Subjectname", info.SubjectId);
        return View(info);
        }
    

    查看创建:

    @if (TempData["thanks"] != null)
    {
        <div class="alert alert-success">
            @TempData["thanks"]
        </div>
    }
    
    // your form
    

    【讨论】:

    • 我不确定我是否理解你的答案,我已将我的 ajax 更改为 :$('#contact').submit(function () { $.ajax({ url: this.action, type: this.method, data: $(this).serialize(), //success: function (result) { // $('#contact').html(result); success: function(_bla) //MyParttailViewName { $('alert alert-success') }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus + " Error: " + errorThrown); } }); });
    • 但是这是什么> info.ShouldRenderPartialView = true;帮手?
    • 好的,你使用的是ajax,对吧?我会更新我的答案
    • 如果可能的话,我更喜欢 c# 解决方案
    • 看看我的编辑答案。我相信“bla”正在继承主布局
    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多