Forms 从控制器加载部分视图
请容忍我,我正在学习asp.net MVC 3:) 我已经搜索了大量论坛帖子,但我真的找不到适合我的项目的好解决方案 情景: 我有一个_布局,它呈现我的身体。在我目前正在努力解决的主体中,我有一个局部视图,它通过ajax.actionlink和占位符div更新 主视图(缩短) 结论性问题 如何从控制器中的actionmethod\u addProduct在主视图中的div id=“container”中呈现部分的\u addProductForms 从控制器加载部分视图,forms,asp.net-mvc-3,asp.net-ajax,asp.net-mvc-partialview,Forms,Asp.net Mvc 3,Asp.net Ajax,Asp.net Mvc Partialview,请容忍我,我正在学习asp.net MVC 3:) 我已经搜索了大量论坛帖子,但我真的找不到适合我的项目的好解决方案 情景: 我有一个_布局,它呈现我的身体。在我目前正在努力解决的主体中,我有一个局部视图,它通过ajax.actionlink和占位符div更新 主视图(缩短) 结论性问题 如何从控制器中的actionmethod\u addProduct在主视图中的div id=“container”中呈现部分的\u addProduct 提前感谢您。在您的部分视图中,您当前拥有一个标准的Htm
提前感谢您。在您的部分视图中,您当前拥有一个标准的
Html.BeginForm
,它呈现一个正常的
标记,并向服务器进行完整的回发。这解释了为什么您没有得到AJAX调用
另一方面,此表单需要将文件上载到服务器。但正如您所知,您无法使用AJAX将文件上载到服务器。当然,除非您使用一些javascript文件上传插件,例如,或。顺便说一下,在支持AJAX的现代浏览器中,您可以使用AJAX请求将文件上载到服务器。前面提到的插件基本上简化了检测客户端浏览器功能的任务,并在必要时使用回退机制(如旧浏览器的隐藏iFrame)
总之,您需要使用javascript将文件上传到服务器并保持在同一页面上
<div class="mini-menu-content">
Product
<ul>
<li>
@Ajax.ActionLink("Aanmaken", "_addProduct", new { type = "Beheer/Add/_addProduct" },
new AjaxOptions
{
UpdateTargetId = "container",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET"
})
</li>
</ul>
</div>
<div id="container">@Html.Partial("Beheer/_welkom")
</div>
@model BlokCWebwinkelGroep3.Models.Product
@using (Html.BeginForm("_addProduct", "Beheer", FormMethod.Post,
new
{
enctype = "multipart/form-data",
id = "Form",
name = "Form"
}))
{
//All the input fields for the model
<div class="editor-label">
Plaatje:
</div>
<div class="editor-field">
<input type="file" name="imageFile" required="true" />
<input type="submit" value="Aanmaken" />
</div>
}
public ActionResult Beheer()
{
return View();
}
public ActionResult _addProduct(string type)
{
return PartialView(type);
}
[HttpPost]
public ActionResult _addProduct(HttpPostedFileBase imageFile, Product product)
{
if (ModelState.IsValid && (imageFile != null && imageFile.ContentLength > 0))
{
//Save to folder, get path, call dbcontroller and save all of it in db.
return PartialView("Beheer/_welkom");
}
else
{
return PartialView("Beheer/Add/_addProduct", product);
}
}