I am developing a little application as exercise to learn MVC and ASP.NET, and I have a problem with the following code:
BTW: Sorry... I studied .NET but not ASP, just Windows Forms and nothing about MVC (as a practical way).
This is my View:
@using (Html.BeginForm("userById", "Home", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Id)
@Html.ValidationMessageFor(model => model.Id)
<button type="submit">Subscribe</button>
}
<h3>Value results</h3>
@Html.TextArea("results", new { rows = 40, columns = 80 })
And I want to fill this TextArea (can be another element) with a Json Data
My controller:
[HttpPost]
public ActionResult userById()
{
string receivedData;
HttpWebRequest req = (HttpWebRequest)WebRequest
.Create("http://www.mocky.io/v2/5808862710000087232b75ac");
req.ContentType = "application/json";
var response = (HttpWebResponse)req.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
receivedData = sr.ReadToEnd();
}
var data = JsonConvert.DeserializeObject<RootObject>(receivedData);
List<User> userList = data.clients.ToList();
foreach (var item in userList)
{
ViewData["results"] = item.Id;
}
return View("Index");
}
My idea is populate the TextArea with the Json data that are stored into the variable called userList, but the foreach loop just fill ONE element. :( Can anybody help me?
Thanks in advance!
ViewData["results"] = receivedData;