I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.
In a view, I have 2 DropDownList and TextArea and 2 buttons :
- DropDownList Poste
- DropDownList Fonction
- Button Ajouter
- Button Enregistrer
- TextArea
The Values of the TextArea are added from the DropDownList 'Fonction' when I click on the button 'Ajouter'.
I want to retrieve those values in a local variable. And that when the user click on the button 'Enregistrer'(submit).
This is the code of the view :
<% using (Html.BeginForm("Store", "Fonction")) { %>
<h2>Gérer les droits</h2>
<form id="form1" runat="server">
<fieldset><legend>Gestion</legend>
<div>
<%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems)%>
</div>
<br />
<div>
<%:Html.Label("Fonction :")%><%: Html.DropDownListFor(model => model.SelectedFonction, Model.FoncItems, new { @id = "ff" })%>
</div>
<br />
<div><input type="button" value="Ajouter" id="aj" onclick="addtext()"/></div>
<br />
<div>
<textarea id="tt" cols="10" name="S1" rows="8" readonly="true"></textarea>
</div>
</fieldset>
<br />
<div><input type="submit" value="Enregistrer" id="sv" /></div>
</form>
<script language="javascript" type="text/javascript">
var storedValues = [];
function addtext() {
var ff = document.getElementById('ff');
var tt = document.getElementById('tt');
var selectedValue = ff.options[ff.selectedIndex].value + " ";
if (storedValues.indexOf(selectedValue) === -1) {
storedValues.push(selectedValue)
tt.value = storedValues.join('')
}
}
</script>
and this is the code of the methode store (for the submit) in the controller :
[HttpPost]
public ActionResult Store(FlowViewModel model)
{
if (ModelState.IsValid)
{
Fonction_Poste FP = new Fonction_Poste();
FP.ID_Poste = model.SelectedPoste;
FP.ID_Fonction = model.SelectedFonction;
FP.Droit = 1;
System.Web.UI.HtmlControls.HtmlTextArea htaDemo = (System.Web.UI.HtmlControls.HtmlTextArea)(form1.FindControl("tt"));
String Value = htaDemo.Value;
db.Fonction_Postes.Add(FP);
db.SaveChanges();
}
return RedirectToAction("Index");
}
So as you see, Itried to do retrive values using this statement but that's didn't give any result :
System.Web.UI.HtmlControls.HtmlTextArea htaDemo = (System.Web.UI.HtmlControls.HtmlTextArea)(form1.FindControl("tt"));
String Value = htaDemo.Value;
FlowViewModel
for the value of the text area to bind to. – saravanan May 25 at 17:36