Click here to Skip to main content
15,885,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Programmers, Please bear with me with the way i am going to ask my question because i am learning ASP.NET MVC on my own.


I have two class : Shoe and color.

A shoe will have many colors.

Shoe Entity
C#
public string shoeId{get;set;}
public string BrandName {get;set;}
public string Size {get;set:}
public virtual string ColorId {get;set:}
[ForeignKey("ColorId")]
public virtual ICollection(Color) Colors {get;set;}



Color Entity
C#
public string ColorId {get;set;}
public string ColorName {get;set;}
public string PictureSamnple {get;set;}
public virtual string shoeId {get;set:}


If i scaffold both entitys in the controller, i will have problem with the create method when trying to Create a new Shoe.
If i use @Html.HiddenFor(e => e.ColorId)
@Html.HiddenFor(e => e.shoeId) in the Create view of SHoe, i will get an error when trying to submit.

what i am trying to achive is that when you re creating the shoe, you will also create a color before submitting it but it is kind of difficult. please, help me.
Posted
Updated 14-Oct-15 0:32am
v2
Comments
[no name] 14-Oct-15 6:22am    
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ShoeId,BrandName,Size,ShoeColorId")] Shoe shoe)

from the properties to bind here... how am i going to handle that of the 'ShoeColorId' because i want that items in shoecolor can only be created when creating s 'Shoe'.
That is , you can't create shoecolor on its own except you are creating a new shoe.

Please, programmer help me out.
Suvabrata Roy 14-Oct-15 6:34am    
What kind off error you are facing?
Do you have any property named ShoeColorId?

1 solution

Use fluent api for mapping entities in efficient way. For example you can map the one to many relationship between shoe and colors in following way -

C#
public class ShoeMapper : EntityTypeConfiguration<Shoe>
    {
        public ShoeMapper()
        {
            HasMany(x => x.Colors).WithRequired().HasForeignKey(x => x.ShoeId);
        }
    }


Apart from this you should create a separate table
SQL
tbl_Colors
SQL
[int_Id, varchar_Name]
then a junction table
SQL
tbl_Shoes_Colors
SQL
[fk_Shoes_Id, fk_Colors_Id, varchar_SampleImage]
to map relation as per normalization recommendation.
 
Share this answer
 
Comments
[no name] 14-Oct-15 11:12am    
@model TestingSHOE.Models.Shoe

@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}

Create




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

<div class="form-horizontal">

Shoe




@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ShoeId)
@Html.HiddenFor(model => model.ShoeColorId)

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

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

<div class="form-group">
@Html.LabelFor(model => model.Trekable, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Trekable)
@Html.ValidationMessageFor(model => model.Trekable, "", new { @class = "text-danger" })
</div>
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Comfortable, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Comfortable)
@Html.ValidationMessageFor(model => model.Comfortable, "", new { @class = "text-danger" })
</div>
</div>
</div>



<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create new Shoe" class="btn btn-primary" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}


What i mean to say is that i want to both create the shoe and shoeColor together. Just like in viewModel but how will i handle the foreignKey in the Create method in Controller.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900