[英]Retrieving data from database to text using jquery
I am trying to populate my textboxes in my mvc project but I am missing something which I am not able to understand. 我正在尝试在我的mvc项目中填充文本框,但缺少一些我无法理解的内容。 In my project I have a button, upon clicking that it open a popup model form.
在我的项目中,我有一个按钮,单击后会打开一个弹出模型表单。 In that form I have two textboxes.
在这种形式下,我有两个文本框。 I wanted to populate that so when I open the form textbox is populated with what I wanted to show.
我想填充它,所以当我打开表单文本框时,要填充我想显示的内容。 Can anyone help on how can I do that?
谁能帮助我该怎么做?
my view comtains (.asmx) 我的视图包含(.asmx)
<table align="center">
<tr>
<td valign="top" class="col-label">
Header
</td>
<td class="col-label">
<textarea id="header" name="header" cols="15" rows="2"></textarea>
</td>
</tr>
<tr>
<td valign="top" class="col-label">
Footer
</td>
<td class="col-label">
<textarea id="footer" name="footer" cols="15" rows="2"></textarea>
</td>
</tr>
</table>
My Controller 我的控制器
public ActionResult GetTempelateData(int locationId)
{
var area = _branches.GetById(locationId);
FormTemplate data = new FormTemplate();
data.Header= "";
data.Footer = "";
string query = "";
query = @"SELECT header, footer
from registration_center_template t
Inner Join company_template tc ON t.id = tc.template_id
where tc.location_id =" + locationId.ToString();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["appdb"]))
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.CommandTimeout = 3000;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
data.Header = reader["header"].ToString();
data.Footer = reader["footer"].ToString();
}
}
}
connection.Close();
}
return View("FormTemplate", data);
}
Now how can I add jquery to populate my data into textboxes? 现在如何添加jquery将数据填充到文本框中?
Well, first of all I wouldn't get the form data on button click, but on said "popup modal form" open event, if it has one. 好吧,首先,我不会在单击按钮时获得表单数据,而是在具有“弹出模式表单”打开事件的情况下单击它。 If it doesn't, there should be a single function which handles the opening of the modal, in which case the form data GET should be passed as a callback function to it.
如果没有,则应该有一个函数来处理模式的打开,在这种情况下,表单数据GET应该作为回调函数传递给它。
Secondly, I do not see why AJAX is needed here. 其次,我不明白为什么这里需要AJAX。 Do you have an ever changing header and footer?
您有不断变化的页眉和页脚吗?
So I will provide first the version which I think is more appropriate, and after that the AJAX solution. 因此,我将首先提供我认为更合适的版本,然后提供AJAX解决方案。 ** By the way, I corrected your Action name typo and I'm using Razor MVC
**顺便说一句,我更正了您的动作名称拼写错误,并且我正在使用Razor MVC
1- Using MVC (should be easy to translate to asp.net) 1-使用MVC(应该很容易转换为asp.net)
@model FormTemplate
<table align="center">
<tr>
<td valign="top" class="col-label">
Header
</td>
<td class="col-label">
@Html.TextAreaFor(model => model.Header, new { cols = 15, rows = 2 })
</td>
</tr>
<tr>
<td valign="top" class="col-label">
Footer
</td>
<td class="col-label">
@Html.TextAreaFor(model => model.Footer, new { cols = 15, rows = 2 })
</td>
</tr>
</table>
I assume your view is called FormTemplate, in which case you are already passing the model to it on your GetTemplateData
Action. 我假设您的视图称为FormTemplate,在这种情况下,您已经在
GetTemplateData
Action GetTemplateData
模型传递给它。
2- Using jQuery AJAX 2-使用jQuery AJAX
Model 模型
// This should actually be on a repository class
public static FormTemplate GetFormTemplateById(int locationId)
{
string query =
@"SELECT header, footer
from registration_center_template t
Inner Join company_template tc ON t.id = tc.template_id
where tc.location_id =" + locationId;
using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["appdb"]))
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.CommandTimeout = 3000;
using (SqlDataReader reader = command.ExecuteReader())
{
return reader.Read()
? new FormTemplate
{
Header = reader["header"] as string,
Footer = reader["footer"] as string
}
: new FormTemplate(); // could also be null - preference;
}
}
}
}
Controller 控制者
public JsonResult GetTemplateData(int locationId)
{
return Json(FormTemplate.GetTemplateById(locationId), JsonRequestBehavior.AllowGet);
// if FormTemplate returns null, your javascript will fail
// could use FormTemplate.GetTemplateById(locationId) ?? new FormTemplate()
// or on JavaScript: if (!response) return;
}
JavaScript 的JavaScript
$.get('@Url.Action("GetTemplateData", "Admin")',
{ locationId: @ViewData["locationID"] }
// locationID must be an int or this will fail, either silently or explicitly
)
.done(function (response) {
// if your model can be null, use:
// if (!response) return;
$('#header').val(response.Header);
$('#footer').val(response.Footer);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.