我是MVC的新手,我无法将输入文本的值添加到我创建的viewmodel中。这是我到目前为止所拥有的。
它唯一能做的就是重新加载我的页面而不是真正将值插入我的VM。我也得到错误,我的输入字符串格式不正确。
[UPDATE] 我的ViewModel PO_OrderList
public class PO_OrderList
{
public int Id { get; set; }
public int Qty { get; set; }
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public decimal UnitPrice { get; set; }
public int Amount { get; set; }
}
public class PO_OrderListContext : DBContext
{
public DbSet<PO_OrderList> poOrderList { get; set; }
}
我在哪里输入我的值
@using (Html.BeginForm("AddtopoOrderList", "PurchaseOrder", FormMethod.Post))
{
<td><input type="text" name="Qty" id="txtqty" maxlength="2" size="3" onkeypress="return isNumberKey(event)" /></td>
<td>
<select name="ProductName" id="ProductName">
@foreach (var item in ViewBag.productList)
{
<option id=@item.ProductID value="@item.UnitPrice" data-desc="@item.Description"> @item.ProductName </option>
}
</select>
</td>
<td><input type="text" id="txtdescription" name="Description" /></td>
<td><input type="text" id="txtunitprice" name="Price" /></td>
<td><input type="text" id="txtamount" name="Amount" /></td>
//Button that will submit values in this from to Controller
<td><input type="submit" id="btnAdd" value="ADD" /></td>
}
MY Controller
[HttpPost]
public ActionResult AddtopoOrderList(string Qty, string ProductName, string Description, string UnitPrice, string Amount)
{
POdb.poOrderList.Add(new PO_OrderList
{
Qty = Convert.ToInt32(Qty),
ProductName = ProductName,
Description = Description,
UnitPrice = Convert.ToInt16(UnitPrice),
Amount = Convert.ToInt16(Amount)
});
POdb.SaveChanges();
return RedirectToAction("Index");
}
答案 0 :(得分:0)
更改要提交的按钮类型 input type =“submit”id =“btnAdd”...
答案 1 :(得分:0)
更好的方法是将Properties
绑定到您的表单中,然后将其放在您的控制器上,如下所示:
@using(Html.BeginForm("AddtopoOrderList", "PurchaseOrder", FormMethod.Post)) {
<td>
@Html.TextBoxFor(model=>model.Qty, new{maxlength="2", size="3", onkeypress="return isNumberKey(event)" }
</td>
........
< td > <input type="submit" id="btnAdd" value="ADD" /> < /td>
}
并获取您Controller
ActionResult
的表单值,如下所示:
[HttpPost]
public ActionResult AddtopoOrderList(PO_OrderList model) {
// you will get all your form value into your model
// Do your logic here
return View();
}
但如果你仍然想要遵循当前的方法,那么你必须遵循以下FormCollection
方法:
//以同样的方式保留您的HTML
更改您的ActionResult
,如下所示:
[HttpPost]
public ActionResult AddtopoOrderList(FormCollection formData) {
int Qty = Convert.ToInt32(formData["QTY"].toString()); // you wil get your Input values into formData
// Do your logic here
return View();
}
希望我的代码可以帮助你
谢谢:)
答案 2 :(得分:0)
是更好的方法是在剃刀视图中更改文本框@ Html.TextboxFor bind automaticaly或其他方式是 你可以添加
[HttpPost]
public ActionResult AddtopoOrderList(string Qty, string ProductName, string Description, string Price, string Amount)
{
POdb.poOrderList.Add(new PO_OrderList
{
Qty = Convert.ToInt32(Qty),
ProductName = ProductName,
Description = Description,
UnitPrice = Convert.ToInt16(UnitPrice),
Amount = Convert.ToInt16(Amount)
});
POdb.SaveChanges();
return RedirectToAction("Index");
}
请提供与您给出的文本框名称相同的参数名称,然后将其工作罚款