将新创建的对象传递给局部视图

时间:2014-07-18 19:02:35

标签: asp.net-mvc entity-framework partial-views

这是我的方法的一部分,我创建一个新对象并将其保存到DB:

 var db = new ArtContext();

            var nyArt = new ArtWork()
            {
                ArtLink = name,

            };

            db.ArtWorks.Add(nyArt);
            db.SaveChanges();

            PartialView("_CreatedArt", nyArt);

如您所见,我尝试将新对象传递给局部视图。我希望在哪里呈现部分内容:

@Html.Partial("_CreatedArt", Model)

然而,没有任何反应(没有视图渲染)。是因为DB需要一些时间来保存新对象,还是我可能做错了什么呢?

编辑:

索引视图,我想渲染部分:

@model Heroz.Models.ArtWork
@using Heroz.Models
@{
    ViewBag.Title = "Home Page";
}

    <div class="jumbotron">
        <h1>ASP.NET</h1>
        <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
        <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
    </div>

    <button onclick="drawShapes()"></button>

        <!--Canvas1-->
         <canvas id="canvas" width="400" height="300" style="background-color: slategray;"></canvas>
         <input type="button" id="btnSaves" name="btnSaves" value="Save the canvas to server" />
         <input type="hidden" name="imageData" id="imageData" />

        <!--Canvas2-->
         @Html.Partial("_CreatedArt", Model)

部分观点:

@model Heroz.Models.ArtWork

<div id="canvas" width="400" height="300" style="background-color: grey;">
    <img src="@Model.ArtLink"/>
</div>

我正在使用的课程:

namespace Heroz.Models
{
    public class ArtWork
    {


        public int ID { get; set; }
        public string ArtLink { get; set; }

    }

    public class ArtContext : DbContext
    {
        public DbSet<ArtWork> ArtWorks { get; set; }
    }
}

尝试做什么我做的很简单,用画布清理图片,将其上传到数据库(这很好),然后尽快显示部分图片。

编辑AJAX:

 $("#btnSaves").click(function () {

        var image = document.getElementById("canvas").toDataURL("image/png");
        image = image.replace('data:image/png;base64,', '');

        $.ajax({
            type: 'POST',
            url: "../../Home/UploadImageS",
            data: '{ "imageData" : "' + image + '" }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                alert('Image saved successfully !');
            }
        });
    });

完成行动:

static string path = Path.GetTempFileName();

        public ActionResult UploadImageS(string imageData)
        {
            string fileNameWitPath = path + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
            using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    byte[] data = Convert.FromBase64String(imageData);
                    bw.Write(data);
                    bw.Close();
                }
            }


            string link = UploadImage(fileNameWitPath);

            JObject o = JObject.Parse(link);

            string name = (string)o.SelectToken("data.link");

            var db = new ArtContext();

            var nyArt = new ArtWork()
            {
                ArtLink = name,

            };

            db.ArtWorks.Add(nyArt);
            db.SaveChanges();
            System.IO.File.Delete(fileNameWitPath);

            /*RedirectToAction("Index","Home");*/

            PartialView("_CreatedArt", nyArt);

        }

2 个答案:

答案 0 :(得分:1)

您可以在MVC中链接控制器操作,而不是重定向。

public class MyController {

    public ActionResult MainIndex() {

        FunModel myModel = new FunModel();
        return View("Index", myModel);
    }

    public ActionResult PostAction(string id) {
        //Do db work
        //Do additional work

        // Re-use the logic for loading the main view
        ActionResult result = MainIndex();
        //You can access view model data like this
        FunModel myModel = (FunModel)ViewData.Model;
        myModel.UpdatedId = id;
        return result;
    }
}

您可以通过从ViewData.Model属性中读取视图数据来访问上一个操作中的视图数据。

答案 1 :(得分:0)

当您返回局部视图时,您可以将html响应替换为元素html,如:

$.ajax({
            type: 'POST',
            url: "../../Home/UploadImageS",
            data: '{ "imageData" : "' + image + '" }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                $("#ContainerId").html(msg);
            }
        });