0

I have a model in my project that contains appropriate code for fetching data from my database and it stores the data in DataTable object.

In the controller, I've extracted the data from DataTable in an array of string as follows

Controller:

DataTable infotable = new DataTable();
infotable = pfd.getProfileData(userid); //getProfileData is my method in model class
foreach (DataRow row in infotable.Rows) {

            studentinfo[0] = row["studentId"].ToString();
            studentinfo[1] = row["firstName"].ToString();
            studentinfo[2] = row["lastName"].ToString();
            studentinfo[3] = row["password"].ToString();
            studentinfo[4] = row["email"].ToString();
            studentinfo[5] = row["contactNo"].ToString();
            studentinfo[6] = (Convert.ToDateTime(row["regDate"])).ToShortDateString();
        }
        ViewData["studentinfo"] = studentinfo;
        return View();

In the view the code that I've used to display the data is ViewBag goes like:

 @foreach (string info in ViewData["studentinfo"] as string[])
 {
      <p>@info</p>    
 }

This set of code works perfectly fine and shows the required output.

Now I want the data in the ViewBag back in an array of String.

Or possibly I can pass the String itself to the view from controller, if so how can I access the passed string array in View?

5
  • 1
    i think if you need to access the data furthermore, the easiest way would be to put it into the session. Storing the datatable into the session: Session["infotable"]=infotable; To get the data from the session use : DataTable info = (DataTable)Session["infotable"]; Commented Sep 11, 2014 at 7:09
  • Please Explain - "Now I what the data in the ViewBag back in an array of String" Commented Sep 11, 2014 at 7:10
  • @user1519979 this seems legitimate. And then I access the DataTable in the usual way as I've already done, right? Commented Sep 11, 2014 at 7:18
  • @Typist Editted that 'what' to 'want' Commented Sep 11, 2014 at 7:19
  • right, just cast the object of the session to use it on the usual way Commented Sep 11, 2014 at 7:25

2 Answers 2

2

You are already accessing the passed string array in the view. ViewData["studentinfo" is your string array. Though, what I suspect you want to do is use a typed view. Here's what I suggest:

First make a model type for student info (maybe named StudentInfo), something like this:

public class StudentInfo
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    ....
    ....
}

Then, change you code where you apply the array to this:

var studentinfo = new StudentInfo();
foreach (DataRow row in infotable.Rows) {

    studentinfo.Id = row["studentId"].ToString();
    studentinfo.FirstName = row["firstName"].ToString();
    ....
    ....
}
return View(studentinfo);

Then, in your view, add this at the top

@model StudentInfo // might need to include full namespace

And to print out the values you do

@Model.FirstName

Now, if you still want to send it as an array, you still can, simply change the view @model StudentInfo to @model string[] and pass in your array to View in the controller, then you can treat Model as a string array in the view.

Sign up to request clarification or add additional context in comments.

2 Comments

I've already made an instance of my class profileData(here mentioned by you as StudentInfo) in the Controller. Is it necessary that i use var studentInfo = new StudentInfo(); instead of Studentinfo si = new StudentInfo(); ?
If you already have an instance, then just use that instance. Also, var a = new B() is the same as B a = new B().
0

You can use like this in View

@model string [] 

and in controller pass your string array

 return View(studentinfo);

Now use like this

 @foreach (string info in Model)
 {
    <p>
       @info
    </p>    
 }

1 Comment

can you show me some proper code. I'm not understanding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.