Hi, I am trying to convert an array of strings to a list and post it to my database using Edit Post. The n I want to retrieve it latter using Edit Get. Finally sending it to my view in a viewbag.
Here’s how I thought it should work but I can’t seem to get it right.
The Model:
publicclassEnrollment
{
public virtualList<String>
listOfDays { get;
set;
}
}
Edit post and get
[HttpPost]
public ActionResult Edit(int id, string[] daysInList)
{
var enrollment = new Enrollment();
enrollment.listOfDays = daysInList.ToList();
}
[HttpGet]
public ActionResult Edit(int id, String name, string[] searchString)
{
var classdays = from e in db.Enrollments where e.StudentID == id select e;
var days = classdays.ToList();
viewbag.daysList=days
}
My problem is even though I did this… enrollment.listOfDays = daysInList.ToList();…back in EditPost, it comes back as null in my EditGet.
What am I doing wrong? I can’t seem to find anything on forums that is using my methods. Can someone help me with this or know of a similar tutorial that may be able to help me?
Sorry the split() doesn’t work because the array is not separated with “,”. It is already in the state that one would need to use Split() for. I think my problem is in the model. I track the list variable in the immediate window and I can see that is
has the list of values all the way to the point when I assign it to the model like this:
enrollment.listOfDays = daysInList.ToList();
I think after this point like when I try to access it again like this..
var classdays = from e in db.Enrollments;
classDays.ListOfDays is null which I assume means that the values were not committed to the model.
Do you mean even though the ActionResult is receiving a string array that I should treat it as a single that has to be split? Isn't the array already split?
Do you mean even though the ActionResult is receiving a string array that I should treat it as a single that has to be split? Isn't the array already split?
Yes,
First pass the value of a whole string and then Split inside.
I’m still a bit new to MVC so forgive my misunderstanding. Here’s what I have: In my view I use a dropdown list that looks like this.
Model.Enrollments.FirstOrDefault().WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals(selectedday) }))
In the controller, this list comes through as a string array like ‘String[] searchString’. By the time it gets to the controller it’s already a list in form of an array. Do you mean to convert it to a string somewhere along the way only to then convert
it back to a list using Split()?
In the controller the method signature looks like this:
public ActionResult Edit(string[] searchString)
It doesn’t allow me to split the searchString variable. Any idea what I’m doing wrong?
In the controller, this list comes through as a string array like ‘String[] searchString’. By the time it gets to the controller it’s already a list in form of an array. Do you mean to convert it to a string somewhere along the way only to then convert it
back to a list using Split()?
Hi,
I mean you can input into a TextBox with the value like "a,b,c,d,e" so as to let the Controller accpet the value, and then inside the Controller itself, you can accept the string and Split it by an English comma.
I'd like to make it work with the listed generated by my selectlist rather than rewriting with text boxes. Do you think it has to do with Entollment.Add()?
I'd like to make it work with the listed generated by my selectlist rather than rewriting with text boxes. Do you think it has to do with Entollment.Add()?
Hello again,
So you mean you have a Dropdownlist that lets you mul-choose and you can choose some of them and get result?
AlexanderBla...
Member
325 Points
309 Posts
How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 02, 2012 08:14 PM|LINK
Hi, I am trying to convert an array of strings to a list and post it to my database using Edit Post. The n I want to retrieve it latter using Edit Get. Finally sending it to my view in a viewbag.
Here’s how I thought it should work but I can’t seem to get it right.
The Model:
public class Enrollment
{
public virtual List<String> listOfDays { get; set; }
}
Edit post and get
[HttpPost]
public ActionResult Edit(int id, string[] daysInList)
{
var enrollment = new Enrollment();
enrollment.listOfDays = daysInList.ToList();
}
[HttpGet]
public ActionResult Edit(int id, String name, string[] searchString)
{
var classdays = from e in db.Enrollments where e.StudentID == id select e;
var days = classdays.ToList();
viewbag.daysList=days
}
My problem is even though I did this… enrollment.listOfDays = daysInList.ToList();…back in EditPost, it comes back as null in my EditGet.
What am I doing wrong? I can’t seem to find anything on forums that is using my methods. Can someone help me with this or know of a similar tutorial that may be able to help me?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 03, 2012 08:26 AM|LINK
Hi,
You cannot accept the value here, I think you can just write a string seperated with a comma, and then you can use Split method to cope with that:
[HttpPost] public ActionResult Edit(int id, string daysInList) { var enrollment = new Enrollment(); string[]values = daysInList.Split(','); enrollment.listOfDays = daysInList.ToList(); }AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 08, 2012 12:01 AM|LINK
Sorry the split() doesn’t work because the array is not separated with “,”. It is already in the state that one would need to use Split() for. I think my problem is in the model. I track the list variable in the immediate window and I can see that is has the list of values all the way to the point when I assign it to the model like this:
enrollment.listOfDays = daysInList.ToList();
I think after this point like when I try to access it again like this..
var classdays = from e in db.Enrollments;
classDays.ListOfDays is null which I assume means that the values were not committed to the model.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 08, 2012 12:38 AM|LINK
Yes, I think you have to split the inputted thing as a whole string and then inside the function of Controller itself you can do what you wanna.
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 08, 2012 06:24 AM|LINK
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 08, 2012 06:27 AM|LINK
Yes,
First pass the value of a whole string and then Split inside.
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 08, 2012 06:47 PM|LINK
I’m still a bit new to MVC so forgive my misunderstanding. Here’s what I have: In my view I use a dropdown list that looks like this.
Model.Enrollments.FirstOrDefault().WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals(selectedday) }))
In the controller, this list comes through as a string array like ‘String[] searchString’. By the time it gets to the controller it’s already a list in form of an array. Do you mean to convert it to a string somewhere along the way only to then convert it back to a list using Split()?
In the controller the method signature looks like this:
public ActionResult Edit(string[] searchString)
It doesn’t allow me to split the searchString variable. Any idea what I’m doing wrong?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 08, 2012 11:15 PM|LINK
Hi,
I mean you can input into a TextBox with the value like "a,b,c,d,e" so as to let the Controller accpet the value, and then inside the Controller itself, you can accept the string and Split it by an English comma.
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 09, 2012 06:11 AM|LINK
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How can I use EditPost and EditGet to send and receive string array to and from my database?
Nov 09, 2012 07:09 AM|LINK
Hello again,
So you mean you have a Dropdownlist that lets you mul-choose and you can choose some of them and get result?