I am developing an MVC project and I have the task to select more than one item in checkbox and want to save these selected item to database. For this I have created three table. First one is DistrictMaster
public partial class DistrictMaster
{
public int Id { get; set; }
public string DistrictName { get; set; }
public virtual ICollection PostMasters { get; set; }
}
Here Iam entering various districts and this saves to database second table PostMaster
public partial class PostMaster
{
public int Id { get; set; }
public string PostName { get; set; }
public string PIN { get; set; }
public Nullable DistrictID { get; set; }
public virtual DistrictMaster DistrictMaster { get; set; }
}
here I am entering different post based on the district selected from drop down list . The DistrictID is Foreign key and will save the pincode and names to the corresponding district in table
The third table is AgentPostMapping
public partial class AgentPostMapping
{
public int Id { get; set; }
public Nullable AgentId { get; set; }
public Nullable PostId { get; set; }
public Nullable EffectDate { get; set; }
}
Here the PostId is not foreign key and I need to save the selected or checked items in checkbox as interger to this postId. This is the part where Iam trouble in doing.
controller
public ActionResult Create()
{
ViewBag.AgentId = new SelectList(db.AgentMasters, "ID", "FirstName");
ViewBag.DistrictId = new SelectList(db.DistrictMasters, "Id", "DistrictName");
When I hit on the create button by selecting the checkbox the selected checkbox is not listing in [HttpPost] of create parameter.How can I save the selected checkbox value as integer to db ?
why is that the value not showing in PostId. Can anyone please help me find a solution for this. How can I solve this ???
7 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
I think Its's not Possible to store a comma seperated list to the int field.
You have to change a Data Type of that filed (int to varchar) if you want to store list. then you can follow belows steps.
just you have to implement foreach loop and hit db with where condition to get the id's of selected Postnames and add into an array and insert to field in your table as comma seperated value.
Now Iam getting the selected checkbox item as string , but I need the Id instead of String and want to store these selected Id to PostId field in AgentpostMapping table as coma seperated list, because my PostId is integer. Is it possible ?
As per shared code by you making sense that you have to store values of PostId into AgentPostMapping table. But how you insert multiple PostId to single record of AgentPostMapping table.Either you have to create association table of AgentPostMapping and PostMaster table or You can insert whole string of your hidden field value into db if you dont want to create association table but its not a proper way.
Have you maintain hidden field beacuse all the value will you get from that hidden field to your controller's action. Please debug and see if value is binding properly then you can save accordingly into your db.
And make sure about all the id's beacuse the shared code is working properly from my side
But when I run the code with the changes as you said
@Vaibhav Deshmukh the code is working but the main thing I need is to save the selected checkbox to database, this is not working and also the the value is not passing to the controller. how can I solve this problem ?
Vaibhav DeshmukhPosted Apr 10, 2017, 6:03 AM
');
Vaibhav DeshmukhPosted Apr 11, 2017, 4:22 AM
athira kPosted Apr 11, 2017, 2:27 AM
Vaibhav DeshmukhPosted Apr 11, 2017, 2:06 AM
athira kPosted Apr 11, 2017, 1:46 AM
Vaibhav DeshmukhPosted Apr 11, 2017, 1:31 AM
athira kPosted Apr 10, 2017, 6:54 AM