我试图在javascript网址中传递Html.Textbox值,但是它给出了错误。
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /WebProduct/Add/1
以下是我的观点课程。我将值传递给我的控制器。
已编辑
@model IEnumerable<DatabaseService_WebAPI.Models.ProductType>
@{
ViewBag.Title = "Tablets";
<script type="text/javascript">
$(function () {
$('#edit').click(function () {
var name = $('#quantity').val();
this.href = this.href + '&quantity=' + encodeURIComponent(name);
});
});
</script>
}
<h2>Tablets</h2>
@using (Html.BeginForm("Add", "WebProduct", FormMethod.Post))
{
@Html.ValidationSummary(true)
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Batch)
</th>
<th>
@Html.DisplayNameFor(model => model.Expiry)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Batch)
</td>
<td>
@Html.DisplayFor(modelItem => item.Expiry)
</td>
<td>
@Html.TextBox("quantity")
</td>
<td>
@Html.ActionLink("Add", "Add", new { name = item.Name, type = item.Type }, new { id = "edit" })
</td>
</tr>
}
</table>
}
<div>
@Html.ActionLink("Back to List", "Create")
</div>
它是我的控制器方法,我在其中传递值。
[HttpPost]
public ActionResult Add(string quantity, string name, string type)
{
Product product = new Product();
if (type=="Tablet")
{
//string order = type.Name + " " + type.Quantity;
LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name);
product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User;
//product.OrderDetail = order;
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("TypeT", "WebProduct");
}
else if (type == "Syrup")
{
//string order = type.Name + " " + type.Quantity;
LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name);
product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User;
// product.OrderDetail = order;
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("TypeS", "WebProduct");
}
else
{
// string order = type.Name + " " + type.Quantity;
LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name);
product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User;
// product.OrderDetail = order;
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("TypeC", "WebProduct");
}
return View();
}
此时我不想使用按钮选项。因为我想将数据库记录和用户输入发送到我的控制器方法。
答案 0 :(得分:0)
该行
this.href = this.href + '?quantity=' + encodeURIComponent(name);
您的网址会读取类似
的内容http://localhost:54745/WebProduct/Add?id=1&name=myname&type=mytype?quantity=4
两个问号不能在同一个url查询字符串中,如此
更新: 你也在foreach循环中,意味着你将有多个id为'quantity'的文本框和多个id为'edit'的添加链接
更新2: 在click事件上添加参数'e'以调用'e.preventDefault()'这将阻止链接转到其URL。 那么你还需要设置window.location,而不是this.href(这是链接URL)
$(function () {
$('#edit').click(function (e) {
e.preventDefault();
var name = $('#quantity').val();
window.location = this.href + '&quantity=' + encodeURIComponent(name);
});
});
</script>
答案 1 :(得分:0)
我的理解是你的
this.href == localhost:3325/WebProduct/Add?name=Panadol&type=Tablet
如果这是正确的,则以下内容应该起作用
$(function ()
{
$('#edit').click(function ()
{
var name = $('#quantity').val();
var href = this.href;
var splitHalfUrl = href.split('?');
var splitQueryString = splitHalfUrl[1].split('&');
window.location = "/WebProduct/Add?" + "quantity=" + encodeURIComponent(name) // Quantity
"&" + splitQueryString[0] + // Name
"&" + splitQueryString[1]; // Type
});
});
答案 2 :(得分:0)
好的事情我想指出,你需要防止ActionLink上的默认行为:
<script type="text/javascript">
$(function () {
$('#edit').click(function(event) {
event.preventDefault();
var name = $('#quantity').val();
window.location = this.href + '&quantity=' + encodeURIComponent(name);
});
});
</script>
这应该将您的网页重定向到您想要的网址:localhost:port/WebProduct/Add?name=Name&type=Type&quantity=Quantity
如果您收到其他错误,请检查您的控制器操作是否设置正确(拼写错误可能是个婊子)。