我通常在控制器内更新实体,如下所示:-
[HttpPost]
public ActionResult Create(Project project)
{
//var model = new CompositeImageModel(0);
if (ModelState.IsValid)
{
//var model = new CompositeImageModel(project.ProjectID);
db.Projects.Add(project);
db.SaveChanges();
ViewBag.ProjectID = project.ProjectID;
return RedirectToAction("Index");
}
return View();
}
我希望在我的 Jquery AJAX 代码中获取这个新的 ID“ViewBag.ProjectID”:-
onSaveBegin: function () {
//CODE FOR SAVE BEGIN
$('#imageList').fadeIn('slow');
$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/222"); });
},
而不是硬编码的“222”。
我怎么才能得到它?
感谢您的帮助和时间
** * ** * ****更新* ** * ** * ** * ** * ** * ** * ** * ** * *** 所以我更新了我的 Jquery 如下:-
<script type="text/javascript">
$(document).ready(function () {
var imageList = $('#imageList'),
submitButt = $('#submitButt');
//hide the imageList initially
imageList.hide(); //hide all but the first paragraph
});
var Project = {
save: function () {
var projectForm = $("#projectForm");
if (projectForm.valid()) {
projectForm.submit();
}
else {
Project.onInvalidForm();
}
},
onInvalidForm: function () {
//CODE FOR INVALID FORM
},
onSaveBegin: function () {
//CODE FOR SAVE BEGIN
$('#imageList').fadeIn('slow');
},
onSaveSuccess: function () {
//CODE FOR SAVE SUCCESS
},
onSaveFailure: function () {
//CODE FOR SAVE FAILURE
alert(ViewBag.ProjectID);
},
onSaveComplete: function () {
//CODE FOR SAVE COMPLETE
//var hfProjectId = $("#hfProjectId").val();
//$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); });
}
};
我已将代码移至 onSaveComplete,因为那时我应该拥有 ProjectID,但仍然无法获得它。但是,这两个选项都不起作用。
** * ** * ****更新 2** * ** * ** * ** * ** * ** * ** * ** * ** *
@model MvcCommons.Models.Project
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
}
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript">
$(document).ready(function () {
var imageList = $('#imageList'),
submitButt = $('#submitButt');
//hide the imageList initially
imageList.hide(); //hide all but the first paragraph
});
var Project = {
save: function () {
var projectForm = $("#projectForm");
if (projectForm.valid()) {
projectForm.submit();
}
else {
Project.onInvalidForm();
}
},
onInvalidForm: function () {
//CODE FOR INVALID FORM
},
onSaveBegin: function () {
//CODE FOR SAVE BEGIN
$('#imageList').fadeIn('slow');
},
onSaveSuccess: function () {
//CODE FOR SAVE SUCCESS
},
onSaveFailure: function () {
//CODE FOR SAVE FAILURE
alert(ViewBag.ProjectID);
},
onSaveComplete: function () {
//CODE FOR SAVE COMPLETE
//var hfProjectId = $("#hfProjectId").val();
//$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); });
}
};
@{ var projectID = int.Parse(ViewBag.ProjectID);
}
@using (Ajax.BeginForm(
"/Create",
new { },
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "projectFormContainer",
OnComplete = "Project.onSaveComplete",
OnFailure = "Project.onSaveFailure", OnSuccess = " Project.onSaveSuccess", OnBegin = "Project.onSaveBegin"
},
new { id = "projectForm", name = "projectForm" }))
{
@Html.Hidden("hfProjectId", ViewBag.ProjectID)
<div class="editor-label">
@Html.LabelFor(m => m.ProjectTitle)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.ProjectTitle)
@Html.ValidationMessageFor(m => m.ProjectTitle)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ProjectText)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.ProjectText)
@Html.ValidationMessageFor(m => m.ProjectText)
</div>
<p>
<input type="submit" value="Submit" onclick="Project.save();" />
</p>
</div>
}
<div id='imageList'>
<h2>Upload Image(s)</h2>
@{
Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel((int)ViewBag.ProjectID));
}
</div>
@Html.ActionLink("返回列表", "索引")
使用最新更改更新代码
** * ** * ** * **更新* ** * ** * ** * ** * ** * ** * ** * ***
模式更新:- 现在将 @Html.Hidden 更改为:-
@if(Model != null)
{
@Html.Hidden("hfProjectId", Model.ProjectID)
}
Javascript 还是一样的 :=
onSaveComplete: function () {
//CODE FOR SAVE COMPLETE
var hfProjectId = $("#hfProjectId").val();
alert(hfProjectId);
$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
//$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); });
}
更改 var projectID
@{
if (Model != null)
{
var projectID = Model.ProjectID;
}
}
在控制器中,返回一个带有模型的“视图”
[HttpPost]
public ActionResult Create(Project project)
{
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
ViewBag.ProjectID = project.ProjectID;
//return RedirectToAction("Index");
return View("Create", project);
}
return View();
}
不过还是没有运气!
** * ** * ** * *最终更新* ** * ** * ** * ** * ** * ** * ** * ** * *** 我决定创建一个项目视图模型和尝试通过它传递变量,因为 ViewBag 在我的情况下不起作用。所以我将控制器中的 HttpPost Create 函数更改为如下所示:-
[HttpPost]
public ActionResult Create(Project project)
{
ProjectModel viewModel = new ProjectModel();
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
viewModel.Project = project;
return View("Index", viewModel);
}
return View();
}
但是这仍然没有刷新 ProjectID,所以我决定暂时放弃它并尝试提出更好的设计。但是,如果您看到我做错了,请对此发表评论。
这是我的最终创建视图:-
@model MvcCommons.ViewModels.ProjectModel
@{
var projectID = Model.Project.ProjectID;
}
@{ ViewBag.Title = "创建"; }
创造
@{
Layout = "~/Views/Shared/_Layout.cshtml";
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
}
`
` $(document).ready(function () {
var imageList = $('#imageList'),
submitButt = $('#submitButt');
//hide the imageList initially
imageList.hide(); //hide all but the first paragraph
});
var Project = {
save: function () {
var projectForm = $("#projectForm");
if (projectForm.valid()) {
projectForm.submit();
}
else {
Project.onInvalidForm();
}
},
onInvalidForm: function () {
//CODE FOR INVALID FORM
},
onSaveBegin: function () {
//CODE FOR SAVE BEGIN
alert('onSaveBegin ' + @(projectID));
$('#imageList').fadeIn('slow');
},
onSaveSuccess: function () {
//CODE FOR SAVE SUCCESS
//alert('onSaveSuccess')
},
onSaveFailure: function () {
//CODE FOR SAVE FAILURE
//alert('onSaveFailure ' + @(projectID));
},
onSaveComplete: function () {
//CODE FOR SAVE COMPLETE
//alert('onSaveComplete ' + @(projectID))
//$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(projectID)); });
$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
}
};
@using (Ajax.BeginForm(
"/Create",
new { },
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "projectFormContainer",
OnComplete = "Project.onSaveComplete",
OnFailure = "Project.onSaveFailure",
OnSuccess = "Project.onSaveSuccess",
OnBegin = "Project.onSaveBegin"
},
new { id = "projectForm", name = "projectForm" }))
{
<div>
@*@Html.Hidden("hfProjectId", projectID)*@
@Html.Hidden("hfProjectId", Model.Project.ProjectID)
<div class="editor-label">
@Html.LabelFor(m => m.Project.ProjectTitle)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Project.ProjectTitle)
@Html.ValidationMessageFor(m => m.Project.ProjectTitle)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Project.ProjectText)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Project.ProjectText)
@Html.ValidationMessageFor(m => m.Project.ProjectText)
</div>
<p>
@* *@
}
<div id='imageList'>
<h2>Upload Image(s)</h2>
@{
//Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel((int)ViewBag.ProjectID));
Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel(projectID));
}
</div>
@Html.ActionLink("返回列表", "索引")