POST请求和获取设置操作MVC
本文关键字:操作 MVC 设置 获取 请求 求和 POST | 更新日期: 2023-09-27 17:57:41
我似乎想不出这个,我相信它很简单。每当我按下"更新/保存"按钮时,我的代码就会转到我的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; }
}
以下是SchedulelingController.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()
永远不会被触发。
在将模型分配给视图之前,请确保实例化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并让您的repo负责填充和消化POST后的Order条目来绕过对Order嵌套类的需求。
所以,我不确定这是如何解决问题的。但它确实(目前…仍在等待更多的错误,稍后^_^)
我用将代码封装在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 "";
}
}
如果有人能向我解释为什么这样做,那就太好了,我会给你"答案"。