score:0
first, when asking about issues like this always post your entity classes, not your table structure. your table structure is irrelevant as it is not necessarily the same as what's going on in your code.
that said, the issue here is like due to modelstate
. see, mvc composes an object called modelstate
with data from a variety of sources: namely model
, request
and viewdata
/viewbag
. mvc uses the modelstate
object to render form field values, handle validation, etc. that means that values in viewbag
can and will override values from the post body and on the model instance itself.
as a result, anything you put in to viewbag
should not be named similarly to anything on your model, or you'll end up with weird behavior. in this instance, because you're setting a select list in viewbag.id
, this select list will be used as the value for your model's id
property, which is obviously not correct. just change the name to something unique like viewbag.bagchoices
.
finally, it's a very bad idea to use your entity class directly to post to, along with using bind
to limit properties can be posted. first, bind
requires passing property names as strings, which means you now have something you need to maintain. if you add or remove a property or change the name of a property, then suddenly your form will start behaving incorrectly, or you'll open a security hole, unless you remember to change every usage of bind
as well. let me save you the suspense: you will forget to do that.
additionally, posting to an entity directly is often the source of great confusion because even though it looks and feels like an entity instance, it's actually just a regular old class instance on postback. it is not connected in any way to what's going on in entity framework, and unless you handle it just right, you'll often end up raising runtime exceptions from trying to interact with other actual entities from entity framework. again, allow me to save you suspense: you will mess that up occasionally.
it's better to use a view model, a class which contains only the properties of your entity you want to be modified. you then map your entity to/from this view model. not only is this safer, since everything remains strongly-typed, but it forces you to follow best practices such as never writing user-provided data directly to a database. it also allows you to add additional properties that have nothing to do with what needs to be persisted to a database, such as a property to hold your select list. the entity should only be concerned with business rules that apply to persistence. a view model allows you to apply business rules regarding the view, without interfering with or caring about persistence.
score:0
you have assigned viewbag.id twice on your actionresult edit for post method.
viewbag.id = new selectlist(db.bag, "id", "brand", student.bag.id);
viewbag.id = new selectlist(db.students, "id", "nama", student.id);
this might cause an issue when the view is displayed after a failed post.
on your edit.cshtml for student,
@html.dropdownlistfor(m => m.brand, (selectlist)viewbag.id)
to make sure that the brand is being assigned on your student.
i am assuming that in the student model there is a property named brand with type int or whatever the type of your student.id is.
score:0
transfer data to the page via model. for example you pass student on the view
return view(student);
create a class of model and add student and add your lists. viewbag not always good. for example:
public class mymodel
{
public student student { get; set; }
public selectlist baglist { get; set; }
public selectlist studentslist { get; set; }
}
and pass data to the view:
var model = new mymodel();
model.baglist = new selectlist(db.bag, "id", "brand", student.bag.id);
model.studentslist = new selectlist(db.students, "id", "nama", student.id);
model.student = student;
return view(model);
and on your view add:
@model /*your namespace*/mymodel
and change all methods where using view. and you have assigned viewbag.id twice. sorry for my english.
Source: stackoverflow.com
Related Query
- Entity Framework updating two tables with one to many relationship
- entity framework 4.1 two tables with the same property name
- Sample .NET C# MVC project using Entity Framework with Database First, Multi-Tie architeture and Role-based security?
- Using Entity Framework with two seperate database (Development and Production)
- ASP.NET MVC - How to Deploy to Azure using Entity Framework Model/Database First?
- How do I create a primary key using two foreign keys in Entity Framework 5 code first?
- How to create tables in sql database with Entity framework at runtime and update model at runtime?
- How to use SqlDependency and SignalR with 2 different databases using one function in C#?
- How to pass a list of data to a stored procedure, using Entity Framework 6
- How to map identifying relationship in Entity Framework 5 code first child entity with multiple mutually exclusive parent entities
- How to return two arrays using one WHERE statement
- How to load entity from multiple tables with Dapper?
- In ASP.NET MVC 3 with built-in user login using Forms, how can I retrieve a list of currently logged in users?
- How do I join tables with a condition using LLBLGen?
- Saving images to database with asp.net mvc 4 + Entity Framework
- How to integrate MySQL with .NET Core 3 and Entity Framework 6
- ASP.NET MVC Image Upload and create records on database with Entity Framework
- How to force outer join with .Include query Entity Framework 6
- Using Moq with Entity Framework 6 - Mocking Include and Where
- How to validate a different property that the one changed using IDataErrorInfo with WPF without calling property changed?
- How does the MVC framework validate a code first POCO with DataAnnotations?
- How to Get Full Object with Code First Entity Framework 4.1
- How can I perform database migration using Entity Framework 4.2?
- How to access the database in Middleware using Entity Framework 6
- How to aggregate two worksheets to one workbook using openXML?
- How to structure projects with ASP MVC using Repository Pattern, Service Pattern, UnitOfWork, ORM (EF, NHibernate etc..)?
- How to use spatial type with Entity Framework code-first without referencing Entity Framework
- How to load related entities with the Entity Framework Core
- ASP.net MVC with Entity Framework reordering database records
- How will I use breeze with Entity Framework 6 with .net 4.0
More Query from same tag
- Ist there a way to serialize public properties of a class that implements ICollection
- Problem in tiling image starting at different height using TextureBrush in C#
- Multithreaded Directory Looping in C#
- WCF - Binary Encoding over HTTP throwing client and service binding mismatch exception
- Projecting a sphere onto a cube
- Closing a webpage running in a WPF WebBrowser
- Can't access file from seperate assembly?
- Starting SQL Server via C#
- sharpsvn logmessage edit sharpsvn?
- What's the best way of displaying thumbnails in ASP.NET?
- Date conversion
- How to set color only for the selected frame in collection view using MVVM in Xamarin forms?
- XmlWriter.WriteRaw escapes xml when the writer was created via XElement.CreateWriter
- How to force an asp.net control to reload the postback data?
- How to resolve the same dependency between a child and parent class with structuremap
- How can I organize collections of strings in OOP?
- asp.net error: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
- Screen scraping a mainframe screen in C# *without* 3rd-party utilities
- Anonymous block in C#
- Adding tooltip for RadioButtonList items
- How to set a MenuItem's IsChecked property for a child of type Enum via XAML?
- View Class Diagram Missing
- Entity Framework V4: "Code Only" performance considerations
- Is it safe to include in .gitignore Visual Studio *.tlog files?
- Scrollable ODBC cursor in C#
- C# Explicit Type Casting Enumeration
- MvvmCross android linker kills binding
- WPF increase memory each time open and close window
- System.DirectoryServices is slow
- lock not working in C#