asp.net mvc - get new inserted ID in MVC Controller to jQuery -
i updating entity inside controller follows :-
[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(); } and wish grab new id "viewbag.projectid" inside jquery ajax code :-
onsavebegin: function () { //code save begin $('#imagelist').fadein('slow'); $("#imagelist").click(function () { $("#imagelist").load("/file/imageupload/222"); }); }, instead of hardcoded "222".
how can it?
thanks , time
**********update**************************** have updated jquery follows :-
<script type="text/javascript"> $(document).ready(function () { var imagelist = $('#imagelist'), submitbutt = $('#submitbutt'); //hide imagelist imagelist.hide(); //hide first paragraph }); var project = { save: function () { var projectform = $("#projectform"); if (projectform.valid()) { projectform.submit(); } else { project.oninvalidform(); } }, oninvalidform: function () { //code invalid form }, onsavebegin: function () { //code save begin $('#imagelist').fadein('slow'); }, onsavesuccess: function () { //code save success }, onsavefailure: function () { //code save failure alert(viewbag.projectid); }, onsavecomplete: function () { //code save complete //var hfprojectid = $("#hfprojectid").val(); //$("#imagelist").click(function () { $("#imagelist").load("/file/imageupload/" + hfprojectid); }); $("#imagelist").click(function () { $("#imagelist").load("/file/imageupload/"+@(viewbag.projectid) ); }); } }; i have moved code onsavecomplete, since should have projectid then, still not able yet. both options not work.
**********update 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 imagelist imagelist.hide(); //hide first paragraph }); var project = { save: function () { var projectform = $("#projectform"); if (projectform.valid()) { projectform.submit(); } else { project.oninvalidform(); } }, oninvalidform: function () { //code invalid form }, onsavebegin: function () { //code save begin $('#imagelist').fadein('slow'); }, onsavesuccess: function () { //code save success }, onsavefailure: function () { //code save failure alert(viewbag.projectid); }, onsavecomplete: function () { //code 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("back list", "index") code updated latest changes
***********updated*************************
mode updates :- change @html.hidden :-
@if(model != null) { @html.hidden("hfprojectid", model.projectid) } the javascript still same :=
onsavecomplete: function () { //code 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) ); }); } change var projectid
@{ if (model != null) { var projectid = model.projectid; } }
in controller, returning "view", model
[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(); } still no luck though!
********** final update **************************** have decided create project view model , try pass variables through it, since viewbag not working in case. changed httppost create function in controller follows :-
[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(); } however still did not refresh projectid, , have decided abandon time being , try come better design. if see soemthign wrong doing please comment on this.
this final create view :-
@model mvccommons.viewmodels.projectmodel @{ var projectid = model.project.projectid; }
@{ viewbag.title = "create"; }
create
@{ layout = "~/views/shared/_layout.cshtml"; html.enableclientvalidation(); html.enableunobtrusivejavascript(); }
`
` $(document).ready(function () {
var imagelist = $('#imagelist'), submitbutt = $('#submitbutt'); //hide imagelist imagelist.hide(); //hide first paragraph }); var project = { save: function () { var projectform = $("#projectform"); if (projectform.valid()) { projectform.submit(); } else { project.oninvalidform(); } }, oninvalidform: function () { //code invalid form }, onsavebegin: function () { //code save begin alert('onsavebegin ' + @(projectid)); $('#imagelist').fadein('slow'); }, onsavesuccess: function () { //code save success //alert('onsavesuccess') }, onsavefailure: function () { //code save failure //alert('onsavefailure ' + @(projectid)); }, onsavecomplete: function () { //code 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("back list", "index")
easy way (not sure it)
$("#imagelist").click(function () { $("#imagelist").load("/file/imageupload/"+@(viewbag.projectid) ); }); the normal way
1- put hiddenfield inside view
2- value viewbag.projectid , assign hidden field
3- read jquery , use wherever want.
an example should eb added answer right away.
example:
update
// projectid value var projectid = int.parse(viewbag.projectid); // add hiddenfield , set it's value projectid @html.hidden("hfprojectid", projectid) // inside jquery code, hiddenfield value var hfprojectid = $("#hfprojectid").val(); // use value inside code $("#imagelist").click(function () { $("#imagelist").load("/file/imageupload/"+hfprojectid ); });
Comments
Post a Comment