[英]Asp.net MVC Dynamically set TextArea
I'm working on an Asp.net MVC5 application. 我正在研究Asp.net MVC5应用程序。
I need to write some XML into a textarea, so it could be parsed by some JavaScript later in my project. 我需要将一些XML写入文本区域,以便稍后在我的项目中可以通过一些JavaScript对其进行解析。 As of now I have loaded the XML info into a ViewBag and was wondering how I could dynamically set a textarea with this information.
到目前为止,我已经将XML信息加载到ViewBag中,并且想知道如何使用此信息动态设置文本区域。
my controller (Index): 我的控制器(索引):
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Tasks.xml");
ViewBag.xml = doc.InnerXml();
Thanks, any help will be greatly appreciated. 谢谢,任何帮助将不胜感激。
-- html form
@Html.TextArea("xml")
<input type="submit" value="Save" />
-- html form
post action 行动后
[HttpPost]
public Actionresult SomeAction(string xml){...}
better solution (using strongly typed views) 更好的解决方案(使用强类型视图)
model 模型
public class XmlViewModel
{
public string Xml { get; set; }
}
controller 调节器
public Actionresult SomeAction()
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Tasks.xml");
var model = new XmlViewModel
{
Xml = doc.InnerXml();
}
return View(model);
}
[HttpPost]
public Actionresult SomeAction(XmlViewModel model)
{
...
return View(model);
}
view 视图
@model XmlViewModel
-- html form
@Html.TextAreaFor(x => x.Xml)
<input type="submit" value="Save" />
-- html form
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.