我似乎无法想出这一个,我确信它很简单。 每当我按下“更新/保存”按钮,我的代码就会进入我的Order类来执行{get; set}。但是,它将我的PhonePrimary字段传递为“null”,这会导致错误。
以下是EditOrder.cshtml页面
@using (Html.BeginForm())
{
<div class="form-group-PhonePrimary">
<div class="input-group" style="border-right:0px">
<span class="input-group-addon" style="text-align: left; border-right: 0px; min-width: 115px">Phone #</span>
@Html.TextBoxFor(model => model.order.PhonePrimary, new { @class = "form-control", @maxlength = "13" })
@Html.ValidationMessageFor(model => model.order.PhonePrimary)
</div>
</div>
<div class="form-group" style="padding-left:5px">
<div class="btnUpdateSave">
<input type="submit" value="Update/Save" class="btn btn-primary col-md-10" />
</div>
</div>
}
以下是Order类。
public class Order
{
private string _PhonePrimary;
[DisplayName("Phone Primary")]
public string PhonePrimary
{
get
{
//this._PhonePrimary = BeautifyPhoneNumber(this._PhonePrimary);
return BeautifyPhoneNumber(this._PhonePrimary);
}
set
{
this._PhonePrimary = value;
}
}
}
以下是EditOrder.cshtml页面中使用的EditOrderViewModel
public class EditOrderViewModel
{
public Order order { get; set; }
public List<Status> StatusOptions { get; set; }
public IEnumerable<Document> Documents { get; set; }
public IEnumerable<Notary> Notaries { get; set; }
public Notary NotaryAssigned { get; set; }
}
下面是SchedulingController.cs中的httpget和httppost
[HttpGet]
public ActionResult EditOrder(int id)
{
var model = repositoryScheduling.Order_FindById(id);//returns EditOrderViewModel
model.NotaryAssigned = repositoryScheduling.Notary_FindAssignedNotary(model.order.Id);
return View(model);
}
[HttpPost]
public ActionResult EditOrder(EditOrderViewModel orderModel)
{
try
{
repositoryScheduling.Order_Update(orderModel);
return RedirectToAction("OrderIndex");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
return View();
}
}
这是SchedulingREpository.cs中的Order_FindById函数。我正在使用Dapper来帮助处理SQL命令。
public EditOrderViewModel Order_FindById(int id)
{
var parameters = new DynamicParameters();
parameters.Add("@ID", value: id);
var query = @"long query string that i dont want to put";
using (var multi = this.db.QueryMultiple(query, parameters))
{
EditOrderViewModel editVM = new EditOrderViewModel();
editVM.order = multi.Read<Order>().SingleOrDefault();
editVM.StatusOptions = multi.Read<Status>().ToList();
editVM.Documents = multi.Read<Document>().ToList();
return editVM;
}
}
每当我切换到使用model.order._PhonePrimary
而不是当前代码中的model.order.PhonePrimary
时(BeautifyNumber()
)。我没有收到任何错误,但我的{{1}}永远不会被触发。
答案 0 :(得分:0)
确保在将模型分配给视图之前实例化Order属性。
例如:
public ActionResult EditOrder() {
var vm = new EditOrderViewModel();
vm.Order = new Order();
return View(vm);
}
编辑:在你的回购中试试这个:
editVM.order = multi.Read<Order>().SingleOrDefault() ?? new Order();
编辑2:这可能是一个模型绑定问题。您可以考虑使用自定义模型绑定器进行POST操作,或者您可以通过直接在视图模型中引用PhonePrimary来绕过对Order嵌套类的需要,并让您的repo在POST后将其填充并消化为Order条目。
答案 1 :(得分:0)
所以,我不确定这是如何解决这个问题的。但它确实(现在......仍在等待更多涉及此事的错误^ _ ^)
我使用
将代码封装在BeautifyPhoneNumber()
函数中
if (numberToBeautifuy != null)
{
//code
}
else
{
return "";
}
完成的功能如下。
public static String BeautifyPhoneNumber(string numberToBeautify)
{
if (numberToBeautify != null)
{
//The below gives us capture groups for each
//individual piece of the number.
var regularExpression = new Regex(@"(\d{3})(\d{3})(\d{4})(x\d*)?");
//This matches a number that's already been beautified,
//so we can guard against beautifying twice.
var alreadyBeautifulExpression = new Regex(@"(\(\d{3}\)) (\d{3})-(\d{4}) ?(x\d*)?");
var beautifulNumber = string.Empty;
var separator = "-";
var space = " ";
//This prevents us from accidentally beautifying
//something more than once
//You could also guard against this in your getter using a
//IsBeautified extension, using the alreadyBeautifulExpression above
if (alreadyBeautifulExpression.IsMatch(numberToBeautify))
{
return numberToBeautify;
}
//Trying to protect against invalid input... May be insufficient,
//Or unnecessary
if (string.IsNullOrEmpty(numberToBeautify)
|| regularExpression.Matches(numberToBeautify).Count <= 0)
{
return beautifulNumber;
}
GroupCollection groups = regularExpression.Matches(
numberToBeautify)[0].Groups;
//More protection against invalid input
if (groups.Count <= 3)
{
return beautifulNumber;
}
//Given "7689131234",
beautifulNumber += "(" + groups[1] + ")" + space; //gives us "(768) "
beautifulNumber += groups[2] + separator; //gives us "(768) 913-"
beautifulNumber += groups[3]; //gives us "(768) 913-1234"
//If we have an extension, we add it.
if (groups[4] != null)
{
beautifulNumber += space + groups[4];
}
return beautifulNumber;
}
else
{
return "";
}
}
如果有人可以向我解释为什么会有效,那就太棒了,我会给你答案&#39;分。