尽管我在其他问题中找到了所有建议,this article POST后,传递给视图的 List vsValues 始终为空。
查看
@model OTS.ParcelOrder
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ParcelOrder</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.otsID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.otsID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.otsID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.parcelID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.parcelID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.parcelID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.recipientCountry, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.recipientCountry, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.recipientCountry, "", new { @class = "text-danger" })
</div>
</div>
@for (int i = 0; i < Model.vsValues.Count; i++)
{
@Html.Label(Model.ParcelOrder_VSFields.ElementAt(i).VendorSpecifiedInfoField.fieldName,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.vsValues[i], new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.vsValues[i], "", new { @class = "text-danger" })
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
控制器
// GET: ParcelOrders/Create
public ActionResult Create(int vendorId = 1)
{
ParcelOrder order = new ParcelOrder(vendorId);
return View(order);
}
// POST: ParcelOrders/Create
// Aktivieren Sie zum Schutz vor übermäßigem Senden von Angriffen die spezifischen Eigenschaften, mit denen eine Bindung erfolgen soll. Weitere Informationen
// finden Sie unter http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ParcelOrder parcelOrder)
{
parcelOrder.customerID = User.Identity.GetUserId();
if (ModelState.IsValid)
{
db.ParcelOrder.Add(parcelOrder);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(parcelOrder);
}
public partial class ParcelOrder
{
private Entities db = new Entities();
public List<string> vsValues = new List<string>();
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ParcelOrder()
{
this.ParcelOrder_VSFields = new List<ParcelOrder_VSFields>();
}
public ParcelOrder(int vendorId)
{
this.ParcelOrder_VSFields = new List<ParcelOrder_VSFields>();
var vendorQuery = from vsif in db.VendorSpecifiedInfoField
where vsif.vendorID == vendorId
select vsif;
foreach (var vsif in vendorQuery)
{
vsValues.Add("");
this.ParcelOrder_VSFields.Add(new OTS.ParcelOrder_VSFields
{
vsFieldID = vsif.id,
VendorSpecifiedInfoField = vsif,
value = ""
});
}
}
public string otsID { get; set; }
public string parcelID { get; set; }
public string customerID { get; set; }
public string recipientCountry { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ParcelOrder_VSFields> ParcelOrder_VSFields { get; set; }
}
}
注意 这些值应该在列表vsValues 中进行POST,稍后将在控制器内设置为 ParcelOrder_VSFields 的属性,以避免POST冗余信息。
答案 0 :(得分:1)
这是因为你没有在任何地方发布任何内容
data Delayed a = Some a | None deriving (Typeable, Show)
应该像:
test0 :: Typeable a => Delayed a -> String
test0 (Some x) = maybe "not a String" id $ cast x
test0 None = "None"
test0' =
let c = castToDelayed test0 in
[ c (toDyn (None :: Delayed Int))
, c (toDyn (Some 'a'))
, c (toDyn (Some "a")) ]
<强>更新强>
之后。您的模型对我来说是错误的,如果您想将值传递给列表,我建议您具有相同类型的列表属性[此属性需要在您的模型@using (Html.BeginForm())
中而不是虚拟]。然后在类的无参数构造函数中执行foreach。在您看到问题的每一步中跟踪它。