Data |
Database, data model, data access. Implement of new data fields.
This topic contains the following sections:
This section contains the following subsections:
Select the database project
Open database table in designer
Write a new field: field name, data type, when need size, allows null or not, default value
Example:
[KilometerSale] FLOAT NOT NULL DEFAULT 0, [Reason] INT NOT NULL DEFAULT 0, [KilometerPurchase] FLOAT NOT NULL DEFAULT 0
Save and build database project
Publish database project. Load and select profile to publish.
Message: Rows were detected. The schema update is terminating because data loss might occur. Reason: exist database with data records can't update schema, so manual update to database. |
Select the data model project
Open folder "Models" and data model CS file
Write data field as property
Example:
/// <summary> /// Kilometer on purchase /// </summary> [Display(Name = "Kilometer Verkauf")] public double KilometerPurchase { get; set; } = 0; /// <summary> /// Kilometer on sale /// </summary> [Display(Name = "Kilometer Kauf")] public double KilometerSale { get; set; } = 0; /// <summary> /// Reason of sale /// </summary> [Display(Name = "Grund")] public Velo.Data.Enumerations.Reasons Reason { get; set; } = Enumerations.Reasons.Kein;
Complete field in constructor with record parameter.
Example:
/// <summary> /// Initializes a new instance of the BikeHistory class. /// Constructor with parameter of data record to fill this data item /// </summary> /// <param name="pDataRecord">Data record to fill</param> public BikeHistory(System.Data.IDataRecord pDataRecord) { // ... this.KilometerPurchase = pDataRecord.GetDouble(pDataRecord.GetOrdinal("KilometerPurchase")); this.KilometerSale = pDataRecord.GetDouble(pDataRecord.GetOrdinal("KilometerSale")); this.Reason = (Velo.Data.Enumerations.Reasons)pDataRecord.GetInt32(pDataRecord.GetOrdinal("Reason")); }
Code example for enumeration as text. Implement in data model CS file.
/// <summary> /// Reason of sale as text (enumeration) /// </summary> [NotMapped] [Display(Name = "Grund")] public string ReasonText { get { return this.Reason.ToString(); } set { this.Reason = (Enumerations.Reasons)Enum.Parse(typeof(Enumerations.Reasons), value); } }
Select the data model project
Open data access CS file
Addition of fields in the methods: Update and Add
Example method Update:
//* update data item * SqlDataAdapter lDataAdapter = new SqlDataAdapter(); lCommand = new SqlCommand("UPDATE BikeHistory SET BikeID = ..., KilometerPurchase = @KilometerPurchase, KilometerSale = @KilometerSale, Reason = @Reason WHERE ID = @ID", lConnection); // ... lCommand.Parameters.Add(new SqlParameter("@KilometerPurchase", pBikeHistory.KilometerPurchase)); lCommand.Parameters.Add(new SqlParameter("@KilometerSale", pBikeHistory.KilometerSale)); lCommand.Parameters.Add(new SqlParameter("@Reason", pBikeHistory.Reason)); lDataAdapter.UpdateCommand = lCommand; // ...
Select the web project
Open view CSHTML file
Addition of HTML controls in the view: list, display and edit forms
Example: web view display
<!-- ... ---> <div class="form-group"> <div class="col-md-3"> @Html.DisplayNameFor(model => model.KilometerPurchase) </div> <div class="col-md-9"> <div class="Text-Box100Right">@Model.KilometerPurchase.ToDecimal(0)</div> </div> </div> <div class="form-group"> <div class="col-md-3"> @Html.DisplayNameFor(model => model.KilometerSale) </div> <div class="col-md-9"> <div class="Text-Box100Right">@Model.KilometerSale.ToDecimal(0)</div> </div> </div> <div class="form-group"> <div class="col-md-3"> @Html.DisplayNameFor(model => model.Reason) </div> <div class="col-md-9"> <div class="Text-Box100Right">@Model.Reason</div> </div> </div> <!-- ... --->
Code view example for enumeration selection. Implement in view CSHTML file.
@Html.DropDownListFor(model => model.Reason, new SelectList(Enum.GetValues(typeof(Velo.Data.Enumerations.Reasons))), new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Reason, "", new { @class = "text-danger" })
Java script file is a code of behind view client code. |
Open the Java script file
Addition field declaration
Code example of field declaration
//#region --- declaration data fields --- // set by "BikeHistory.js" function DialogBikeHistoryGetData // ... var BikeHistoryKilometerPurchase = 0; var BikeHistoryKilometerSale = 0; var BikeHistoryKilometerReasons = 0; //#endregion
Addition field in method save
Code example:
function DialogBikeHistorySave() {
// example add event handler to continue process
// document.getElementById("DialogBikeHistoryButtonSave").addEventListener("click", FunctionName);
$('#DialogBikeHistory').modal('hide')
DialogBikeHistoryGetData(); // get values from dialog
// save values
var lValues = { ..., KilometerPurchase: BikeHistoryKilometerPurchase, KilometerSale: BikeHistoryKilometerSale, Reason: BikeHistoryReason };
var lURL = getURLRoot() + 'api/BikeHistoryData/Save';
// asynchronous post - waiting next processing
$.ajax({
url: lURL,
type: 'POST',
data: lValues,
async: false,
success: function (data) {
if (BikeHistoryID == 0) {
BikeHistoryID = data.ID;
}
},
error: function (xhr, status, error) {
alert("#Exception of method DialogBikeHistorySave: " + xhr + ", " + status + ", " + error);
}
});
}Addition field in method get value from HTML elements
Code example:
function DialogBikeHistoryGetData() {
// set data field variable BikeHistory* from HTML element in dialog bike history
// get HTML elements
var lElements = document.getElementById("DialogBikeHistoryContent");
// get HTML element "input"
var lElementInputs = lElements.getElementsByTagName('input');
for (var lIndex = 0; lIndex < lElementInputs.length; ++lIndex) {
if (lElementInputs[lIndex].hasAttribute("id")) {
var lIDName = lElementInputs[lIndex].getAttribute("id");
var lValue = lElementInputs[lIndex].value;
switch (lIDName) {
// ...
case "KilometerPurchase": BikeHistoryKilometerPurchase = lValue; break;
case "KilometerSale": BikeHistoryKilometerSale = lValue; break;
case "Reason": BikeHistoryReason = lValue; break;
default:
}
}
}
}Open the web controller
Addition field value from form to data model
Code example in method SetData from web controller
private Velo.Data.Models.BikeHistory SetData(Velo.Data.Models.BikeHistory pBikeHistory, FormCollection pCollection) { // ... pBikeHistory.KilometerPurchase = Convert.ToDouble(pCollection["KilometerPurchase"]); pBikeHistory.KilometerSale = Convert.ToDouble(pCollection["KilometerSale"]); pBikeHistory.Reason = (Velo.Data.Enumerations.Reasons)Enum.Parse(typeof(Velo.Data.Enumerations.Reasons), pCollection["Reason"]); return pBikeHistory; }