[英]How could I pull data from several db tables to automate data entry for user in ASP.NET MVC 5 application?
I have a form in my ASP.NET MVC 5 application that is made up of several different entities that are stored in my database. 我的ASP.NET MVC 5应用程序中有一个表单,该表单由存储在数据库中的几个不同实体组成。 I am trying to set it up so that, when a user types in the name of a particular entity (let's say a Shipper
), the fields in the form that pertain to that entity (eg the fields in the Shipper
section) are automatically populated with that Shipper's
data. 我正在尝试进行设置,以便当用户键入特定实体的名称(例如Shipper
)时,与该实体相关的表单中的字段(例如Shipper
部分中的字段)会自动填充与该Shipper's
数据。 Here is what I have tried: 这是我尝试过的:
I wrote a class to connect to the database that returns a dataset
of Shipper
data. 我编写了一个类来连接到数据库,该数据库返回Shipper
数据的dataset
。
public class DbConnection
{
private string _connectionString = "the connection string";
public DbConnection()
{
}
public DataSet GetShipperData()
{
DataSet shipperData = new DataSet();
try
{
//TODO: replace command with stored procedure name
using(SqlConnection dbConn = new SqlConnection(_connectionString))
{
using (SqlDataAdapter sda = new SqlDataAdapter("select * from dbo.shipper", dbConn))
{
sda.Fill(shipperData);
}
}
return shipperData;
}
catch
{
return null;
}
}
}
}
Here is the method I've written to get the shipper
dataset
in the controller for the form: 这是我编写的用于在窗体的控制器中获取shipper
dataset
的方法:
public string[] GetShipperData()
{
DbConnection conn = new DbConnection();
var shipperData = conn.GetShipperData();
List<Shipper> listOfShipperData = shipperData.Tables[0].AsEnumerable().Select(datarow => new Shipper { Name = datarow.Field<string>("Name") }).ToList();
return listOfShipperData;
}
Finally, here is the javascript code in the view where the form is located. 最后,这是表单所在视图中的javascript代码。 I'm using jQuery
to call the GetShipperData
method in the controller. 我正在使用jQuery
在控制器中调用GetShipperData
方法。 I have omitted the markup for brevity: 为了简洁起见,我省略了标记:
$("#theShipperField").blur(function () {
$.get('@Url.Action("GetShipperData")', function (data) {
var shipperFields = document.getElementsByClassName('form-control shipper');
$(shipperFields).attr("value", data);
});
});
The first error I'm getting is this: In the Shipper
fields, I am getting this instead of the data I want: System.Collections.Generic.List1[appname.Models.Shipper]
. 我得到的第一个错误是:在“ Shipper
字段中,得到的不是我想要的数据: System.Collections.Generic.List1[appname.Models.Shipper]
。 I think it's because javascript obviously can't work with shipper
data, so I need this to be in the form of strings. 我认为这是因为javascript显然无法处理shipper
数据,因此我需要采用字符串形式。 I'm unsure of how to do this. 我不确定如何执行此操作。 I would be open to doing this a completely different way, so any help is greatly appreciated. 我愿意以完全不同的方式进行此操作,因此,我们将不胜感激。 Please let me know if I can provide any additional information. 请让我知道是否可以提供其他信息。
First: try use Entity Framework Having your db model in Entity framework I should create web api 首先:尝试使用Entity Framework将db模型放入Entity Framework中,然后创建Web API
[System.Web.Http.Route("api/GetShippers")]
[System.Web.Http.HttpGet]
public IEnumerable<Shipper> GetShippers()
{
var shippers= unitOfWork.GetShippers();
return shippers;
}
After that in your js code 之后,在您的js代码中
dataservice.js dataservice.js
var dataService = new function () {
getShippers = function (callback) {
$.getJSON(window.applicationBaseUrl + 'api/GetShippers', function (data)
{
callback(data);
});
};
}
index.cshtml index.cshtml
@section scripts{
<script type="text/javascript">
$(document)
.ready(function() {
dataService.getShippers(DoSomething);}
function DoSomething(shippersData) {console.log(shippersData);}
</script>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.