如何从MVC .NET中的控制器中编写TextArea [英] How to write in a TextArea from controller in MVC .NET
问题描述
我正在开发一个小应用程序作为练习来学习MVC和ASP.NET,并且以下代码存在问题:
I am developing a little application as exercise to learn MVC and ASP.NET, and I have a problem with the following code:
顺便说一句:对不起...我学习.NET但不学习ASP,仅学习Windows Forms,而没有涉及MVC(作为一种实用方法).
BTW: Sorry... I studied .NET but not ASP, just Windows Forms and nothing about MVC (as a practical way).
这是我的视图:
@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 })
我想用一个Json数据填充这个TextArea(可以是另一个元素)
And I want to fill this TextArea (can be another element) with a Json Data
我的控制器:
[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");
}
我的想法是用存储在名为userList的变量中的Json数据填充TextArea,但是foreach循环仅填充ONE元素.:(有人可以帮我吗?
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?
提前谢谢!
推荐答案
最后,我用下面的代码解决了这个问题,如果有人对如何改进它感到很高兴.
Finally I solved the problem with the following code, I will pleasant if anybody have some idea about how can improve it.
控制器代码:
[HttpPost]
public ActionResult userById()
{
string receivedData;
HttpWebRequest req = (HttpWebRequest)WebRequest
.Create("http://www.amisteriousurl.com");
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();
ViewData["results"] = getId(userList);
return View("Index");
}
private string getId(List<User> list)
{
StringBuilder sb = new StringBuilder();
foreach (var item in list)
{
sb.Append(item.Id);
sb.Append("\n");
}
return sb.ToString();
}
查看:
@{
ViewBag.Title = "Home";
}
@model InsuranceWebAPI.Models.User
@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 })
型号:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace InsuranceWebAPI.Models
{
public class RootObject
{
public IEnumerable<User> clients { get; set; }
}
public class User
{
private string email;
private string id;
private string name;
private string role;
public User()
{
}
public User(string email, string id, string name, string role)
{
this.Email = email;
this.Id = id;
this.Name = name;
this.Role = role;
}
[Required]
public string Email { get; set; }
[Required]
public string Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Role { get; set; }
}
}
谢谢大家!
这篇关于如何从MVC .NET中的控制器中编写TextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!