我需要为我的MVC5表单添加一些验证。我已经为模型添加了必需项,但它适用于其他页面,但此页面不同。在视图中我使用了CustomerModel,但由于它可能返回多于1个值,因此页面顶部的模型是CustomerModel列表。因此,当我想要验证行时,我不确定应该如何编写它。它正在为这样的1个值工作:
@Html.ValidationMessageFor(Model => Model.Account_Number);
但现在我的模型是列表,我也尝试过这样但是它给了我错误:
@Html.ValidationMessageFor(Model => Model[1].Account_Number);
这是我的完整观点:
enter code here
@model List<CSAProject.Models.CustomerModel>
@{
ViewBag.Title = "Search By Account Number";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table style="border: none">
<tr>
<td colspan="2" style="text-align: center; font-weight: bold; color: red">@*By RemoteRefNumber*@<br />
</td>
</tr>
<tr>
<td style="text-align: center; padding: 5px;">Enter Account Number:
</td>
<td style="text-align: center; padding: 5px;">
@Html.TextBox("Account_Number")
@Html.ValidationMessageFor(Model => Model[1].Account_Number);
</td>
<td colspan="2" style="text-align: center; font-weight: bold; color: red">
<button type="submit" class="btn btn-success">Search</button></td>
</tr>
</table>
}
@if (Model.Count > 0)
{
<table class="table">
<tr>
<th>Account Number
</th>
<th>First Name
</th>
<th>Last Name
</th>
<th>Last 4SSN
</th>
<th>Zip
</th>
<th>is Registered
</th>
<th>Online Agreement
</th>
</tr>
@foreach (var customer in Model)
{
<tr>
<td>
@customer.Account_Number
</td>
<td>
@customer.First_Name
</td>
<td>
@customer.Last_Name
</td>
<td>
@customer.Last4SSN
</td>
<td>
@customer.Mailing_Zip
</td>
<td>
@if (customer.isRegistered == true)
{
<text>Yes</text>
}
else
{
<text>No</text>
}
</td>
<td>
@if (customer.Online_Agreement == true)
{
<text>Yes</text>
}
else
{
<text>No</text>
}
</td>
<td>@* Name of button, action name,name of controler,parameter of action,class*@
@Html.ActionLink("Edit", "Edit", "CustomerDetail", new { id = customer.Customer_ID }, new { @class = "btn btn-success" })
</td>
</tr>
}
</table>
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
这是我的模特:
public class CustomerModel
{
public int Customer_ID { get; set; }
[Required]
[DisplayName("ACCOUNT NUMBER")]
public string Account_Number { get; set; }
[DisplayName("FIRST NAME")]
public string First_Name { get; set; }
[DisplayName("LAST NAME")]
public string Last_Name { get; set; }
and the rest
}
这是我的控制者:
public ActionResult SearchByAccountNumber(InputDataAccount model)
{
customerServiceUri = ConfigurationManager.AppSettings["url"].ToString();
List<CustomerModel> customerModels = new List<CustomerModel>();
if (ModelState.IsValid)
{
if (model.Account_Number != null)
{
var uri = string.Format("{0}GetCustomerByAccountNumber", customerServiceUri);
// Create an object to convert to JSON
var data = new
{
CustomerData = new
{
Account_Number = model.Account_Number
}
};
// Convert object to byte[]
var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
// Create request to the web service
var req = HttpWebRequest.Create(uri);
//Meta data
req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
// Write the bytes to the web service (POST the data)
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
// THIS is where we call the web service with the posted data
var response = req.GetResponse();
// Create a string container for my JSON response
string jsonResponse = string.Empty;
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
jsonResponse = streamReader.ReadToEnd();
}
}
// Convert the JSON from our response into our response object "SearchByNameResult"
var jsonReult = JsonConvert.DeserializeObject<SearchByAccountNumberResult>(jsonResponse);
// The cutomers is the value of the result property
var customers = jsonReult.GetCustomerByAccountNumberResult;
// Create CustomerModels from our results
foreach (var customer in customers)
{
customerModels.Add(new CustomerModel
{
Customer_ID = Convert.ToInt32(customer.Customer_ID),
Account_Number = customer.Account_Number,
First_Name = customer.First_Name,
Last_Name = customer.Last_Name,
isRegistered = customer.isRegistered,
Online_Agreement = customer.Online_Agreement,
Last4SSN = customer.Last4SSN,
Mailing_Zip = customer.Mailing_Zip
});
}
}
}
return View(customerModels);
}