我有以下 MVC 应用程序,请参阅下面的前端:
“发票日期创建”是一个基于以下方法的 html5 兼容日期选择器:
http://www.aubrett.com/InformationTechnology/WebDevelopment/MVC/DatePickerMVC5.aspx
我对屏幕负载的要求如下:
1) 在日期和文本框更新的日期选择器“onSelect”上,我需要传递此值,例如2014/11/19 并将其操作为 yyyymm 格式(例如 201411)。有一个陷阱。它需要能够为当前时期以及上一时期创造这样的值(value)。
因此,2014/11/19 应使用两个选项填充“发票编号期间”下拉列表,即:
201411(当期)
201410(上一期)
有什么方法可以让我通过这个日期 2014/11/19 例如回到我的 Controller ,在那里我可以进行操作并将 201411 和 201410 返回到我的下拉列表?还是我以错误的方式思考这个问题?我已经阅读了大量 Javascript 文章,但没有一篇文章完全符合我的需要。如何以及最好的方法是什么?
我是一个 MVC 和 Javascript 的新手,所以努力想解决这个问题..
编辑 - 这是我的代码:
Finance.cshtml:
@model List<FinanceMVC.Models.FinanceViewModel>
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Finance", "Finance", FormMethod.Post, new { @class = "form-horizontal" }))
{
<div class="col-md-1 fade in" style="margin-left: 5%; width: 100%; font-size: 11px; height: auto;">
<div id="div_Finance_Container" style="width: 100%; height: 80%; overflow: auto; text-align: left;" runat="server" class="scroll-pane">
<div style="height: 500px; overflow: auto;">
<div><a href="#" id="addNew" class="add-another-cat smallest">Add New</a></div>
<table id="dataTable" border="0">
<tr>
<th>Invoice Type</th>
<th>Invoice Number</th>
<th>Invoice Number Period</th>
<th>Invoice Date Created</th>
<th></th>
</tr>
@if (Model != null && Model.Count > 0)
{
int j = 0;
foreach (var i in Model)
{
<tr>
<td>
<div class="col-md-1" style="width: 20%;">
<select name="ddlInvoiceType">
@foreach (var item in Model)
{
foreach(var bItem in item.InvoiceType)
{
<option value="@bItem.Description">@bItem.Description</option>
}
}
</select>
</div>
</td>
<td>@Html.TextBoxFor(a => a[j].InvoiceNumber)</td>
<td>@Html.TextBoxFor(a => a[j].InvoiceNumberPeriod)</td>
<td>@Html.EditorFor(a => a[j].InvoiceDateCreated)</td>
<td>
@if (j > 0)
{
<a href="#" class="remove">Remove</a>
}
</td>
</tr>
j++;
}
}
</table>
<input type="submit" value="Save Bulk Data" />
</div>
</div>
</div>
}
@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
<script language="javascript">
$(function () {
$(document).on('change', '#InvoiceDateCreated', function () {
alert($(this).val());
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#[0].InvoiceDateCreated').change(function () {
$('#[0].InvoiceDateCreated').text('sam');
});
});
</script>
<script language="javascript">
$(document).ready(function () {
//1. Add new row
$("#addNew").click(function (e) {
e.preventDefault();
var $tableBody = $("#dataTable");
var $trLast = $tableBody.find("tr:last");
var $trNew = $trLast.clone();
var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
$trNew.find("td:last").html('<a href="#" class="remove">Remove</a>');
$.each($trNew.find(':input'), function (i, val) {
// Replaced Name
var oldN = $(this).attr('name');
var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
$(this).attr('name', newN);
//Replaced value
var type = $(this).attr('type');
if (type.toLowerCase() == "text") {
$(this).attr('value', '');
}
// If you have another Type then replace with default value
$(this).removeClass("input-validation-error");
});
$trLast.after($trNew);
// Re-assign Validation
var form = $("form")
.removeData("validator")
.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
});
// 2. Remove
$('a.remove').live("click", function (e) {
e.preventDefault();
$(this).parent().parent().remove();
});
});
</script>
}
Finance.cs(模型):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace FinanceMVC.Models
{
//public class Finance
//{
//}
public partial class FinanceViewModel
{
/*ID*/
[Required]
[Display(Name = "ID")]
public int ID { get; set; }
/*BINL_BIND_ID*/
[Required]
[Display(Name = "Invoice Type")]
//public int InvoiceType { get; set; }
public List<C_InvoiceType> InvoiceType { get; set; }
/*BINL_Inv_Num_Pointer*/
[Required]
[Display(Name = "Invoice Number")]
public char InvoiceNumber { get; set; }
/*BINL_Inv_Num_Period*/
[Required]
[Display(Name = "Invoice Number Period")]
public int InvoiceNumberPeriod { get; set; }
/*BINL_Created*/
[Display(Name = "Invoice Created Date")]
[Required]
[DataType(DataType.DateTime)]
public DateTime InvoiceDateCreated { get; set; }
/*BINL_SystemInserted*/
[Required]
[Display(Name = "Invoice System Inserted")]
public bool InvoiceSystemInserted { get; set; }
}
public class C_InvoiceType
{
public int ID { get; set; }
public string Description { get; set; }
}
}
FinanceController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinanceMVC.Models;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace FinanceMVC.Controllers
{
public class FinanceController : Controller
{
public ActionResult Finance()
{
FinanceViewModel FVM = new FinanceViewModel();
// This is only for show by default one row for insert data to the database
List<FinanceViewModel> FinanceViewList = new List<FinanceViewModel>
{
new FinanceViewModel
{
ID = 0 , InvoiceType = ReturnInvoiceType(), InvoiceNumber = '\0', InvoiceNumberPeriod = 0, InvoiceDateCreated = DateTime.Now, InvoiceSystemInserted = false
}
};
return View(FinanceViewList);
}
//
// GET: /Finance/
/*Matches file name of Finance.cshtml*/
[HttpPost]
public ActionResult Finance(List<FinanceViewModel> model)
{
if (ModelState.IsValid)
{
}
return View(model);
}
//
// GET: /Finance/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
public static List<C_InvoiceType> ReturnInvoiceType()
{
string sQ = ""
+ " Select ID, BIND_Description \n"
+ " From Biller_InvoiceNum_DefSet with(nolock) \n"
;
try
{
using (SqlConnection sCon = new SqlConnection(Application_Info.sMOB_Master_Conn()))
{
if (sCon.State != ConnectionState.Open)
{
sCon.Open();
}
using (SqlCommand sCmd = new SqlCommand(sQ, sCon))
{
using (SqlDataReader sRdr = sCmd.ExecuteReader())
{
List<C_InvoiceType> list_InvoiceType = new List<C_InvoiceType>();
while (sRdr.Read())
{
list_InvoiceType.Add(new C_InvoiceType
{
ID = (int)sRdr["ID"],
Description = (string)sRdr["BIND_Description"]
});
}
return list_InvoiceType;
}
}
}
}
catch
{
}
return ReturnInvoiceType();
}
}
}
请您参考如下方法:
您可以使用 jquery 在客户端执行此操作,而无需调用服务器。由于您有多行,因此您需要为控件提供一个类名,以便可以选择它们,例如
@Html.EditorFor(a => a[j].InvoiceDateCreated, new { htmlAttributes = new { @class = "InvoiceDateCreated" }})
并添加以下脚本
function padLeft(nr, n, str) {
return Array(n - String(nr).length + 1).join(str || '0') + nr;
}
$('.InvoiceDateCreated').change(function () {
var row = $(this).closest('tr');
var date = new Date($(this).val());
var currentMonth = date.getFullYear().toString() + padLeft(date.getMonth() + 1, 2);
date.setMonth(date.getMonth() - 1);
var previousMonth = date.getFullYear().toString() + padLeft(date.getMonth() + 1, 2);
var select = row.find('.InvoiceNumberPeriod').empty();
select.append($('<option></option>').val(currentMonth).text(currentMonth));
select.append($('<option></option>').val(previousMonth).text(previousMonth));
});
