1

I'm trying to get my head around ASP.Net MVC having come from a Web Forms background.

I've got a MySQL database with 2 entities:

artwork - id(PK), artistid(FK), name...
artist - id(PK), artist, website...

I've created an ADO.Net Entity Data Model using this database and ArtistController.cs and ArtworkController.cs, which has created the corresponding views.

Now when I access http://localhost:49664/Artwork, the page displays a list of all the rows in the artwork table, however, it displays the artistid. How would I go about displaying the artist's name instead?

UPDATE:

In the database, the field in the artist table is named 'artist' but when creating the Entity Data Model, Visual Studio has named it as 'artist1'.

I've tried '@Html.DisplayFor(modelItem => item.artist.artist)' but that still displays the artist id.

Artwork class:

public partial class artwork
{
    public int id { get; set; }
    public Nullable<int> artistId { get; set; }
    public string title { get; set; }
    public Nullable<int> editionId { get; set; }

    public virtual artist artist { get; set; }
    public virtual edition edition { get; set; }
}

Artist class:

public partial class artist
{
    public artist()
    {
        this.artworks = new HashSet<artwork>();
    }

    public int id { get; set; }
    public string artist1 { get; set; }
    public string website { get; set; }

    public virtual ICollection<artwork> artworks { get; set; }
}

Artwork Controller:

public ActionResult Index()
{
    var artworks = db.artworks.Include(a => a.artist).Include(a => a.edition);
    return View(artworks.ToList());
}

Artwork view code:

@model IEnumerable<myflatfile.artwork>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
         <th>
            @Html.DisplayNameFor(model => model.artist1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.title)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.artist.artist1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.title)
        </td>
    </tr>
}

</table>
3
  • What is the artist1 column? Based on info provided it should be @Html.DisplayFor(modelItem => item.artist.name) Commented Jun 3, 2015 at 19:39
  • @SteveGreene The artist1 column is the artist's name field I believe. The ADO.Net Entity Data Model created by Visual Studio has named it 'artist1' for some reason instead of 'artist'. Commented Jun 4, 2015 at 7:01
  • right, you can't have a field name with the same name as the class. you might want to rename it 'name' to make it more clear. Commented Jun 4, 2015 at 12:57

3 Answers 3

0

I prepared something for you with notepad and i am a bit sleepy. So, if it has error or errors, write me again for that.

Artwork class:

public class artworklist
{
   public List<artwork> ArtworkModelList { get; set; }
}


public partial class artwork
{
    public int id { get; set; }
    public Nullable<int> artistId { get; set; }
    public string title { get; set; }
    public Nullable<int> editionId { get; set; }

    public virtual artist artist { get; set; }
    public virtual edition edition { get; set; }
}

Artist Class:

public class artistlist
{
    public List<artist> ArtistModelList { get; set; }
}

public partial class artist
{
    public artist()
    {
        this.artworks = new HashSet<artwork>();
    }

    public int id { get; set; }
    public string artist1 { get; set; }
    public string website { get; set; }

    public virtual ICollection<artwork> artworks { get; set; }
}

View:

@model Art.Model.ArtistList



@if (null == Model || null == Model.ArtistModelList || 0 == Model.ArtistModelList.Count)
                {
                    <tr>
                        <td>There is no data.</td>
                    </tr>
                }
                else
                {
                    foreach (var art in Model.ArtistModelList)
                    {
                        <tr>
                            <td>@@@art.artist</td>
                        </tr>
                    }
                }

Controller:

 [HttpGet]
   public ActionResult Art()
   {
           var model = new ArtistList();

            using (var dbContext = new ArtContext())
            {
                var artists = (from m in dbContext.Artists
                             select new ArtistModel
                             {
                                 ID = m.ID,
                                 Artist1 = m.Artist1,
                                 Website = m.Website,
                                 Artworks = m.Artworks
                             }).ToList();

                model.ArtistModelList = artists;
                return View(model);
            }

   [HttpPost]
    public ActionResult Art(Artist model)
    {

            Artist art = new Artist();

            art.ID = model.ID,
            art.Artist1 = model.Artist1,
            art.Website = model.Website,
            art.Artworks = model.Artworks

            using (ArtContext db = new ArtContext())
            {

                db.Artists.Add(art);
                db.SaveChanges();

                return RedirectToAction("*****", "*****"); // where you want (action,url)
            }

        }
Sign up to request clarification or add additional context in comments.

Comments

0

You have used DisplayFor in a wrong way. It is used as a template to display the data (ref).

Should be used as

@Html.DisplayFor(modelItem => modelItem.artist.artist1)

But you have a List for the model. So I'd suggest create a partial view with artwork as model.

@model artwork
<tr>
        <td>
            @Html.DisplayFor(m=> m.artist.artist1)
        </td>
        <td>
            @Html.DisplayFor(m=> m.title)
        </td>
    </tr>

then render this partial view inside your for loop in the main view

@foreach (var item in Model) {
    Html.RenderPartial("partialViewName", item);
}

Or, if you want to keep it simple, just render the properties directly in the main view

@foreach (var item in Model) {
        <tr>
            <td>
                @item.artist.artist1
            </td>
            <td>
                @item.artist.artist1.title
            </td>
        </tr>
    }

Comments

0

It is simple if I got you, you are using entity framework then every artwork entity has the object of artist it is refer to in db. so all you need to do is: @Html.DisplayFor(modelItem => item.artist.name)

Edit: I've a workaround in my mind, you can manipulate the object in Artwork controller before pass it to the view and add the the name and whatever you need

1 Comment

The 'artist' entity has no 'name' field. In the database, it's named 'artist' and the ADO.Net Entity Data Model created by Visual Studio has named it 'artist1'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.