通过模型的ID以partialView为了编辑 [英] Pass model Id to partialView in order to edit

查看:384
本文介绍了通过模型的ID以partialView为了编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有摄影对象的列表,我传入一个观点: -

 公共类摄影
{
    大众摄影()
    {
        NAME =;
        说明=;
        分类=;
        imgUrl的=;
        IsAccordion = FALSE;
    }
    公共字符串名称{;组; }
    公共字符串描述{搞定;组; }
    公共字符串类别{搞定;组; }
    公共字符串imgUrl的{搞定;组; }
    公共BOOL IsAccordion {搞定;组; }
}

在通过列表像这样我的看法我循环:

  @foreach(在Model.Photographys VAR项)
{
    <李班=span3的风格=文本对齐:中心>
        < D​​IV CLASS =缩图-1>
            < H3的风格=保证金底:10px的;> @ item.Name< / H3 GT&;
            < D​​IV CLASS =>
                < D​​IV CLASS =>
                    < IMG SRC =@ item.ImgUrlALT =的风格=能见度:可见;不透明度:1;>
                < / DIV>
            < / DIV>
            <节>
                &所述p为H.; @ item.Description&下; / P>
                < A HREF =#类=BTN BTN-1>了解详情< / A>
                &所述p为H.; @ item.IsAccordion&下; / P>
            < /节>
        < / DIV>
    < /李>
}

我想要做的是有一个让我编辑照片我点击的属性的局部视图。我创建使用scaffoldoption编辑的局部视图。它看起来像这样:

  @model aPhoto_web.Models.AdminPages.Photography@using(Html.BeginForm())
{
    @ Html.AntiForgeryToken()    < D​​IV CLASS =形横>
        < H4>照相机和LT; / H4>
        <小时/>
        @ Html.ValidationSummary(真,新{@class =TEXT-危险})
        < D​​IV CLASS =表单组>
            @ Html.LabelFor(型号=> model.Name,htmlAttributes:新{@class =控制标签COL-MD-2})
            < D​​IV CLASS =COL-MD-10>
                @ Html.EditorFor(型号=> model.Name,新{htmlAttributes = {新@class =表格控}})
                @ Html.ValidationMessageFor(型号=> model.Name,新{@class =TEXT-危险})
            < / DIV>
        < / DIV>
}

等等...

大多数partialViews我读到被直接呈现在parentview得到rendered..That是不是我想要的。我只希望partialView出现时,我在我的照片对象以某种方式传递给它。

我知道这是一个大的问题,但如果有人能在正确的方向指向我it'd是伟大的!谢谢你。

编辑澄清:
看看这个code,我增加了一个的RenderPartial在循环的底部。

  @foreach(在Model.Photographys VAR项)
{
    <李班=span3的风格=文本对齐:中心>        < D​​IV CLASS =缩图-1>
            < H3的风格=保证金底:10px的;> @ item.Name< / H3 GT&;            < D​​IV CLASS =>
                < D​​IV CLASS =>
                    < IMG SRC =@ item.ImgUrlALT =的风格=能见度:可见;不透明度:1;>
                < / DIV>
            < / DIV>            <节>
                &所述p为H.; @ item.Description&下; / P>
                < A HREF =#类=BTN BTN-1>了解详情< / A>
                &所述p为H.; @ item.IsAccordion&下; / P>
            < /节>
        < / DIV>        @ {
            Html.RenderPartial(_ editPhoto项目);
        }
    < /李>
}

这使得局部视图中当然循环每一个项目。我想传递我点击的部分对象的方法...

编辑:

 公众的ActionResult EditPhoto(字符串ID)
{
    变种照片= RavenSession.Load< ContentPage>(ID)为摄影;
    返回PartialView(_ editPhoto,照片);
}


解决方案

首先,你必须增加一个控制器方法,需要摄影名称/ ID(这是更好,如果你能ID属性添加到摄影类)作为参数,并返回部分请查看您创建。

然后当你点击照片,你可以使用jQuery的摄影名称/ ID传递到创建控制器的方法,你可以使用弹出显示结果或特定元素(例如DIV)之内您的页面中。

示例

在循环摄影对象的视图中可以将类和ID属性添加到您的img标签如下:

 < IMG SRC =@ item.ImgUrlALT =级=摄影图像数据imgId =@ item.ID的风格=能见度:可见;不透明度:1;>

然后使用jQuery和jQuery UI可以打开一个对话框,显示局部视图。检查下面的示例code。

  $(.photography图像)。点击(函数(){
    亦即preventDefault();
    $(< D​​IV>< / DIV>中)
        .addClass(对话)
        .appendTo(身体)
        。对话({
            关闭:函数(){$(本)上卸下摆臂()},
            模式:真实,
            高度:500,
            宽度:500
        })
        .load(/ CONTROLER /法photographyId =+ $(本)。数据(imgId));
});

谢谢!

I have a list of Photography objects that I pass into a view:-

Public class Photography
{
    public Photography()
    {
        Name = "";
        Description = "";
        Category = "";
        ImgUrl = "";
        IsAccordion = false;
    }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Category { get; set; }
    public string ImgUrl { get; set; }
    public bool IsAccordion { get; set; }
}

In my view I loop through the list like this:

@foreach (var item in Model.Photographys)
{
    <li class="span3" style="text-align: center">
        <div class="thumbnail thumbnail-1">
            <h3 style="margin-bottom: 10px;">@item.Name</h3>
            <div class="">
                <div class="">
                    <img src="@item.ImgUrl" alt="" style="visibility: visible; opacity: 1;">
                </div>
            </div>
            <section>
                <p>@item.Description</p>
                <a href="#" class="btn btn-1">Read More</a>
                <p>@item.IsAccordion</p>
            </section>
        </div>
    </li>
}

What I want to do is to have a partial view that lets me edit the properties of photo i click. I have created a partial view using the scaffoldoption "Edit"..It looks like this:

@model aPhoto_web.Models.AdminPages.Photography

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Photography</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
}

etc etc...

Most partialViews I read about gets rendered directly when the parentview gets rendered..That is not what I want...I only want the partialView to appear when I somehow pass my Photo-object to it.

I know this is a "big" question but if anyone can point me in the right direction it´d be great! Thanks.

EDIT to clarify: Have a look at this code where I have added an "RenderPartial" at the bottom of the loop.

@foreach (var item in Model.Photographys)
{
    <li class="span3" style="text-align: center">

        <div class="thumbnail thumbnail-1">
            <h3 style="margin-bottom: 10px;">@item.Name</h3>

            <div class="">
                <div class="">
                    <img src="@item.ImgUrl" alt="" style="visibility: visible; opacity: 1;">
                </div>
            </div>

            <section>                          
                <p>@item.Description</p>
                <a href="#" class="btn btn-1">Read More</a>
                <p>@item.IsAccordion</p>
            </section>
        </div>

        @{
            Html.RenderPartial("_editPhoto", item);
        }
    </li>
}

This renders a partial-view for every item in the loop of course. I would like a method that passes the object I click to the partial...

EDIT:

public ActionResult EditPhoto(string id)
{
    var photo = RavenSession.Load<ContentPage>(id) as Photography;
    return PartialView("_editPhoto", photo);  
}

解决方案

First you have to add a controller method which takes Photography Name/ID (It's better if you can add ID property to Photography class) as argument and returns the Partial view you created.

Then when you click the photo you can use Jquery to pass the Photography Name/ID to created controller method and you can display the result using popup or within a particular element (e.g. DIV) inside your page.

EXAMPLE

When you loop Photography objects inside your view you can add a class and ID property to your img tag as follows;

<img src="@item.ImgUrl" alt="" class="photography-image" data-imgId="@item.ID" style="visibility: visible; opacity: 1;">

Then using jquery and jquery UI you can open a dialog box to display the partial view. Check the sample code below.

$( ".photography-image" ).click(function() {
    e.preventDefault();
    $("<div></div>")
        .addClass("dialog")
        .appendTo("body")
        .dialog({
            close: function () { $(this).remove() },
            modal: true,
            height: 500,
            width: 500
        })
        .load("/controler/method?photographyId=" + $(this).data("imgId"));
});

Thanks!

这篇关于通过模型的ID以partialView为了编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆