asp.net mvc 3 - "Null" Foreign key relationship creating record on MVC3 + EF -


I am using EF + MVC 3 + razor, I have a user profile model class whose name, surname variable + foreign The key is hobbies (as I want to see it as a dropdown) I have selected only this clue and data reference to a controller.

When I try to create a record, it is not for me, as they say in the verification area: "Hobby field is required." I want the "Hobby" dropdown not to be needed.

Model:

  Public class UserProfile {[Key] get public int id { Set; } [Required] Public string name {get; Set; } [Required] Public string aliases {get; Set; } Received public harmony hobby; Set; } Public Virtual Hobby Hobbies {get; Set; }} Public Class Hobby {Public Entry Hawkins ID {Received; Set; } Public string HobbieName {get; Set; } Public virtual iconging & lt; UserProfile & gt; UserProfiles {Receive; Set; }} Public class UserProfileDBContext: DbContext {public DbSet & lt; UserProfile & gt; UserProfiles {Receive; Set; } Public DbSet & lt; Hobby & gt; Hobby {get; Set; }}   

See:

  [..] @ Uses (HTML.BeginForm ()) {@ Html.ValidationSummary (true) & lt; Fieldet & gt; & Lt; Story & gt; UserProfile & lt; / Narrative & gt; & Lt; Div class = "editor-label" & gt; @ Html.LabelFor (model => model.Name) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model = & gt; model.Name) @ html.ValidationMessageFor (model = & gt; model.Name) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @HTMLLabelFor (model = & gt; model. Shaniem) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @html Editor (model = & gt; model .name) @html. Validation message (model = & gt; model .name) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @htmllabelfor (model => model.hobbyid, "hobby") & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @html Dropdown List ("Hobbyid", string.optim) @html Validity message (model = & gt; model.hobbyIDID) & lt; / Div & gt; [..]   

The controller is created:

  [http post] Create public anecscent (userprofile user profile) {if (ModelState.IsValid) { Db UserProfiles.Add (userprofile); Db.SaveChanges (); Return Redirect Action ("Index"); } ViewBag.HobbyId = New selection list (DB Hobbies, "Hobby ID", "Hobby", User Profile, Hobby ID); View return (user profile); }   

I tried to modify earlier:

  public class Hobby {public int? HobbiesId {get; Set; }   

But that does not work.

Any help? ..

Thanks advance!

PNP

Add a foreign key constraint as shown below: < / P>

  public int? Hobby {ID}; Set; } [Foreign ("hobby")]    

Comments

Popular posts from this blog

java - NullPointerException for a 2d Array -

python - Assemble mpeg file unable to play in mediaplayer -

c# - NameSpace Manager or XsltContent to parse aspx page -