ASP.NET MVC 4,code首先,查看与多对多的关系创建/编辑对象 [英] ASP.NET MVC 4, Code First, View to Create/Edit Object with Many to Many Relationship

查看:120
本文介绍了ASP.NET MVC 4,code首先,查看与多对多的关系创建/编辑对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型(code第一)及数据库(SQL Server)的设置与多对多的关系和放大器;播种罚款:

I have the model (code first) & database (SQL Server) setup with many to many relationship & seeding fine:

一个地方可以有很多标签(如餐厅,酒吧,咖啡厅) - 和标签可以属于很多地方

A Place can have many tags (eg Restaurant, bar, cafe) - and tags can belong to many places.

Place
[ScaffoldColumn(false)]
public virtual int PlaceID { get; set; }

[Required(ErrorMessage = "Place Name is required")]
[StringLength(100)]
public virtual string Name { get; set; }

[Required]
public virtual string Address {get;set;}

public virtual string ImageLogo {get;set;}
public virtual string ImageBackground {get;set;}


Tag
public virtual int TagID { get; set; }

[Required]
public virtual string Name { get; set; }

[Required]
public virtual string NamePlural { get; set; }

public virtual ICollection<Place> Places { get; set; }

EF迁移产生了与标签识别和功放结合表; PlaceID,我可以在我的种子的方法与添加多个标签精细邻居。大约有50个标签 - 所有的种子

EF Migrations has generated a junction table with TagID & PlaceID and I can add Places with multiple tags fine in my Seed method. There are about 50 tags - all seeded.

我希望做的是有一个创建视图的形式 - 即允许用户从所有的标签选择 - 可能与复选框(打开替代品),一些异步文本框等?然后将其保存模型。

What I would like to do is have a Create view form - that allows users to select from all the tags - maybe with check boxes (open to alternatives), some async textbox etc? Which are then saved with the model.

此数据由管理员进入 - 项的速度是最重要的(并不一定是100%傻瓜证明)

This data will be entered by an administrator - speed of entry is most important (doesn't have to be 100% idiot proof).

我还需要能够编辑的地方 - 和拥有这些标签显示适当删除或添加其他

I also need to be able to edit the place - and have those tags show up appropriately to be removed or others added.

另外要处理的地方缺失的最好方法? - 删除结表中的记录第一个

Also the best way to handle deletion of places - delete records in junction table first?

什么是最好的做法在所有上述的?

What is best practice in all of the above?

感谢。

推荐答案

我觉得你最好的和最简单的方法是,你必须创建一个视图地点和它的底部放了字段集来标记分配给它。

I think the best and simplest way for you is that you have a view for creating Place and at the bottom of it put a fieldset to assign tags to it.

有关字段集,你应该MAVE两个部分观点:一个用于创建和另一个用于编辑。创建局部视图应该是财产以后这样的:

For the fieldset, you should mave two partial views: One for create and another for edit. The partial view for creating should be somthing like this:

@model myPrj.Models.PlaceTagInfo

@{ 
    var index = Guid.NewGuid().ToString(); 
    string ln = (string)ViewBag.ListName;
    string hn = ln + ".Index";
}

<tr>
    <td>        
        <input type="hidden" name="@hn" value="@index" />
        @Html.LabelFor(model => model.TagID)
    </td>
    <td>
        @Html.DropDownList(ln + "[" + index + "].TagID", 
            new SelectList(new myPrj.Models.DbContext().Tags, "ID", "TagName"))
    </td>        
    <td>
        <input type="button" onclick="$(this).parent().parent().remove();" 
            value="Remove" />
    </td>
</tr>

通过调用ajaxly在创建的地方观看这个局部视图,可以呈现每个标签的一些元素。元素的每一行包含一个标签,标签含一个DropDownList和删除按钮简单地删除创建的元素。

By calling this partial view in the create place view ajaxly, you can render some elements for each tag. Each line of elements contains a label, a DropDownList containing tags, and a remove button to simply remove the created elements.

在创建视图的地方,你有一个光秃秃的表将包含那些你通过局部视图创建元素:

In the create place view, you have a bare table which will contain those elements you create through the partial view:

<fieldset>
     <legend>Place Tags</legend>
     @Html.ValidationMessageFor(model => model.Tags)</label>
     <table id="tblTags"></table>                                        
     <input type="button" id="btnAddTag" value="Add new tag"/>
     <img id="imgSpinnerl" src="~/Images/indicator-blue.gif" style="display:none;" />
</fieldset>

和你有以下的脚本来创建一个行每个标签的元素:

and you have the following script to create a line of elements for each tag:

$(document).ready(function () {
    $("#btnAddTag").click(function () {
        $.ajax({
            url: "/Controller/GetPlaceTagRow/Tags",
            type: 'GET', dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                $("#tblTags").append(jqXHR.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $("#tblTags").append(jqXHR.responseText);
            },
            beforeSend: function () { $("#imgSpinnerl").show(); },
            complete: function () { $("#imgSpinnerl").hide(); }
        });
    });
});

操作方法 GetPlaceTagRow 是这样的:

public PartialViewResult GetPlaceTagRow(string id = "")
    {            
        ViewBag.ListName = id;
        return PartialView("_CoursePostPartial");
    }

和您所做的创造......你的问题的整体解决方案有很多codeS视图,局部视图,控制器,Ajax调用和模型绑定。我想只是告诉你,因为我真的不能发布所有的人都在这个答案的方式。

and your done for the create... The whole solution for your question has many codes for views, partial views, controllers, ajax calls and model binding. I tried to just show you the way because I really can't to post all of them in this answer.

希望这个答案是有益的,带路你。

Hope that this answer be useful and lead the way for you.

这篇关于ASP.NET MVC 4,code首先,查看与多对多的关系创建/编辑对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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