score:0

Accepted answer

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.


Related Query

More Query from same tag