Accessing Elements created by Javascript- In code behind in MVC 3
In my cshtml i have defined a tag
I am creating a Image Upload Form, where On File Submit, i am saving the value in a Temp Table(SQL) and returning(GUID) the value thru a Ajax call and Binding the GUID inside Dynamically created DIV Tag(Jquery/Javascript)
<div id="dvDisplayImageParent" class="block clear" runat="server">
</div>
Now, using Javascript i am creating DIV Tags (Multiple) (Containing Images and Textbox) and appending it with dvDisplayImageParent.
I want to access the values in Controller
public ActionResult NewProject(ProjectEditViewModel model, String btnSave)
{// Want to values inside my dvDisplayImageParent }
How can i do this??
My Javascript is as follows :- `var img = document.createElement("IMG"); var lbl = document.createElement("label"); var chk = document.createElement('input'); chk.type = 'checkbox';
//Assign image path to display the image
img.src = imgPath;
img.id = imageID;
img.style.height = "100px";
img.style.width = "100px";
//Appending it with Dynamic DIV
dynDiv.appendChild(img);
var input = document.createElement("input");
//input.setAttribute("type", "hidden");
input.setAttribute("type", "text");
input.setAttribute("name", "hdn" + imageID);
input.setAttribute("value", imageID);
//Image Link
var aElemImageLink = document.createElement('a');
//Move Up Link
var aElemUpLink = document.createElement('a');
//Move Down Link
var aElemDownLink = document.createElement('a');
var callOnImageclick = "DeleteImage('" + imageID + "','" + divDynamicID + "'); return false; ";
aElemImageLink.setAttribute("onclick", callOnImageclick);
aElemImageLink.childNodes = imageID;
var callOnMoveUpclick = "MoveImageUP('" + imageID + "','" + divDynamicID + "'); return false; ";
aElemUpLink.setAttribute("onclick", callOnMoveUpclick);
var callOnMoveDownclick = "MoveImageDOWN('" + imageID + "','" + divDynamicID + "'); return false; ";
aElemDownLink.setAttribute("onclick", callOnMoveDownclick);
//Create a text node to hold the text of the <a>
var aElemTN = document.createTextNode(' Delete Image ');
//Create a text node to hold the text of the <a>
var aElemTNUp = document.createTextNode(' Move UP ');
//Create a text node to hold the text of the <a>
var aElemTNDown = document.createTextNode(' Move DOWN ');
//Append the <a> text node to the <a> element
aElemImageLink.appendChild(aElemTN);
aElemUpLink.appendChild(aElemTNUp);
aElemDownLink.appendChild(aElemTNDown);
//Append the new link to the existing <div>
dynDiv.appendChild(aElemImageLink);
dynDiv.appendChild(aElemUpLink);
dynDiv.appendChild(aElemDownLink);
lbl.id = "lbl" + imageID;
var aElemTNTag = document.createTextNode(' Taggar ');
var aElemTNWantsToshare = document.createTextNode(' Dela? ');
var txtTag = document.createElement("input");
txtTag.id = "txt" + imageID;
txtTag.setAttribute('name', 'Tag');
dynDiv.appendChild(lbl);
dynDiv.appendChild(aElemTNTag);
dynDiv.appendChild(txtTag);
dynDiv.appendChild(chk);
dynDiv.appendChild(aElemTNWantsToshare);
开发者_运维问答 dynDiv.appendChild(input);
//Appending Dynamic DIV with Parent DIV
var parentDiv = document.getElementById("dvDisplayImageParent");
parentDiv.appendChild(dynDiv);`
I Got one alternative :-
string NoOfImages = Convert.ToString(Request.Form["Your Control Name Which You Want To Access"]);
Now, using Request.Form[]
use can get the value's present inside the CONTROL Created using JAVASCRIPT.
But still waiting for a better solution.
> Help Continues...
There is no runat or code behind in mvc. You can't access any elements from controller. I'd suggest watching some of these videos.
精彩评论