0

At the moment I have this as my code:

 public ActionResult Search(SearchModel model)
    {
        string url = "http://10.0.2.31/testwebapi/api/data";
        WebClient wc = new WebClient();

        wc.QueryString.Add("forename", model.Forename);
        wc.QueryString.Add("surname", model.Surname);
        wc.QueryString.Add("caseid", model.CaseId);
        wc.QueryString.Add("postcode", model.Postcode);
        wc.QueryString.Add("telephone", model.Telephone);
        wc.QueryString.Add("email", model.Email);
        wc.QueryString.Add("title", model.Title);

        var data = wc.UploadValues(url, "POST", wc.QueryString);


        var responseString = Encoding.Default.GetString(data);

        return PartialView("~/Views/Shared/SearchResults.cshtml", responseString);

    }

    public class RootObject
    {
        public int Caseid { get; set; }
        public string Title { get; set; }
        public string Forename { get; set; }
        public string Surname { get; set; }
        public string Postcode { get; set; }
        public string Telephone { get; set; }
        public string Email { get; set; }
        public string DOB { get; set; }
        public string Mobile { get; set; }
        public string MaritalStatus { get; set; }
        public string LoanPurpose { get; set; }
        public bool CourtesyCall { get; set; }
        public string isOpen { get; set; }
    }

im not sure where i am going wrong i want it to be able to pull that data from the web api (which returns a json file). then what i want to do is send that data through to my partialview where a @foreach statement will put it into a table but i cant seem to get it to send the data it is interperating it as a string and i dont know why

this is how it is returning:

enter image description here

@model  string
@{ 
    Layout = null;
}

@foreach(var Case in Model)
{
    <p>@Case</p>
}

Partial view code.

This is what my partial now looks like:

@model  IEnumerable<Savvy_CRM_MVC.Models.RootObject>
@{ 
    Layout = null;
}
@foreach (var m in Model)
{
    <p>@m</p>
}

which when i run through the for each is gives me this Savvy_CRM_MVC.Models.RootObject

4
  • 1
    What's the code for your partial view Commented Aug 29, 2017 at 14:05
  • @BurnsBA added it to the bottom for you Commented Aug 29, 2017 at 14:06
  • If you foreach iterate on a string (which is enumerable), you will evaluate each character. Commented Aug 29, 2017 at 14:08
  • how do i change this im very new to MVC Commented Aug 29, 2017 at 14:09

1 Answer 1

1

The problem is that you are binding you model to the raw JSON string.

you need to deserialize the JSON string to your RootObject class and use RootObject as the model for your view.

the following code user Newtonsoft JSON.net to deserialize the string;

var rootObjects = JsonConvert.DeserializeObject<RootObject[]>(responseString);

return PartialView("~/Views/Shared/SearchResults.cshtml", rootObjects);

and you view changes

@model IEnumerable<RootObject>

You are also going to need to change you view to display the properties of your model.

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

8 Comments

Still underlined saying "the type or namespace of 'RootObject' could not be found
You need to fully qualify the RootObject name. I don't know where you have defined it in your project. I only know you defined it because you have it in your question.
RootObject is defined in that class which is DashboardContoller.cs
Move it into the /Models directory and reference it in your controller and your view.
I added a addition to my question because it still isnt working
|

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.