Monday 15 June 2015

How to save the selected value from dropdownlist to database using Entity Framework in ASP.NET MVC 4 -



How to save the selected value from dropdownlist to database using Entity Framework in ASP.NET MVC 4 -

in 1 of view add together dropdown , bind drop downwards database this..

public actionresult postmarks() { joinmod std = new joinmod(); std.drp_bind = (from nm in db.tbstudent select new selectlistitem { text = nm.studentname, value = sqlfunctions.stringconvert((double)nm.studentid).trim() }).tolist<selectlistitem>(); homecoming view(std); }

here dropdownlist in view

<p>studentname</p> @html.dropdownlist("studentname",model.drp_bind,"select")

and on post trying save info database

[httppost] public actionresult postmarks(marks marks) { db.tbmarks.add(marks); db.savechanges(); homecoming redirecttoaction("showallmarks"); }

now when check database after save info in database id save in database 0 dropdownlist. please experts help me solve issue

you should using @html.dropdownlistfor, specifying lambda indicates property in model want bind list's selected value on post.

given view model so:

public class marksviewmodel { public marks marks { get; set; } public ienumerable<selectlistitem> drp_bind { get; set; } }

the drop downwards list in cshtml can declared so:

@html.dropdownlistfor(m => m.marks.studentid, m.drp_bind, "select student")

the first argument look describing property on model want populate selected value drop downwards list in post back. sec list comprising values need info bind.

note cshtml need more form fields within bound other properties of marks property.

your post method take marksviewmodel argument , extract marks property add together dbcontext saving.

entity-framework asp.net-mvc-4 ef-code-first

No comments:

Post a Comment