간단한 색인보기에 포함 된 부분보기가 있습니다. 모델에 새 객체를 추가하고 기존 객체와 함께 새 객체를 표시하기 위해 부분 뷰를 업데이트하려고하면 부분 뷰가 포함 된 페이지 외부로 렌더링됩니까?부분보기가 포함 된 기본보기 내에서 부분보기가 렌더링되지 않습니다 (대신 자체 페이지에서 렌더링 됨).
부분보기를 업데이트하는 데 AJAX를 사용하고 있는데 다음 코드는 무엇이 잘못 되었습니까?
모델 :
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
public class BoringStoreContext
{
List<Product> results = new List<Product>();
public BoringStoreContext()
{
Products = new List<Product>();
Products.Add(new Product() { ID = 1, Name = "Sure", Price = (decimal)(1.10) });
Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
}
public List<Product> Products {get; set;}
}
public class ProductIndexViewModel
{
public Product NewProduct { get; set; }
public IEnumerable<Product> Products { get; set; }
}
Index.cshtml보기 :
@model AjaxPartialPageUpdates.Models.ProductIndexViewModel
@using (Ajax.BeginForm("Index_AddItem", new AjaxOptions { UpdateTargetId = "productList" }))
{
<div>
@Html.LabelFor(model => model.NewProduct.Name)
@Html.EditorFor(model => model.NewProduct.Name)
</div>
<div>
@Html.LabelFor(model => model.NewProduct.Price)
@Html.EditorFor(model => model.NewProduct.Price)
</div>
<div>
<input type="submit" value="Add Product" />
</div>
}
<div id='productList'>
@{ Html.RenderPartial("ProductListControl", Model.Products); }
</div>
ProductListControl.cshtml 부분보기
@model IEnumerable<AjaxPartialPageUpdates.Models.Product>
<table>
<!-- Render the table headers. -->
<tr>
<th>Name</th>
<th>Price</th>
</tr>
<!-- Render the name and price of each product. -->
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(model => item.Name)</td>
<td>@Html.DisplayFor(model => item.Price)</td>
</tr>
}
</table>
컨트롤러 :
public class HomeController : Controller
{
public ActionResult Index()
{
BoringStoreContext db = new BoringStoreContext();
ProductIndexViewModel viewModel = new ProductIndexViewModel
{
NewProduct = new Product(),
Products = db.Products
};
return View(viewModel);
}
public ActionResult Index_AddItem(ProductIndexViewModel viewModel)
{
BoringStoreContext db = new BoringStoreContext();
db.Products.Add(viewModel.NewProduct);
return PartialView("ProductListControl", db.Products);
}
}
,
그래, 나는 스크립트가 완전히 빠져 있었다. 내가 추가 한 후에는 정상적으로 작동했다. 감사! – JaJ
@JaJ 어디서 어떻게 스크립트를 추가하고 있습니까? 나는 당신이 따르는 동일한 예를 따르고 있습니다. 나는이 두 줄을'Index.cshtm'과'ProductListControl.cshtml' 맨 위에 추가했습니다 : ''및 ' 여전히 작동하지 않습니다. – user224567893
@JaJ 그리고 jquery의 어떤 버전을 사용하고 있습니까? – user224567893