Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
share|improve this question
1  
Do you have any property in the FlowViewModel for the value of the text area to bind to. –  saravanan May 25 at 17:36
add comment

1 Answer

up vote 1 down vote accepted

If you have any property in the FlowViewModel for the value of the text area to bind to, you can use it.

If you have any property with the name S1, the value of the text area will be automatically bound.

Because, in ASP.Net MVC, there are no server side controls that you can refer to. Everything is model bound automatically.

Else, you can either create a property or use the FormCollection to get the value and use it accordingly.

Please share your implementation related to this to guide better.

share|improve this answer
 
thx,,,yes I already did that and it' s works but not witth collection but with the request like this : var value1 = Request["S1"]; –  anouar May 25 at 17:54
 
the problem now that the action didn't works (not saving in the base) when the script is functionnal –  anouar May 25 at 17:57
 
what exception are you getting. –  saravanan May 25 at 17:59
1  
Okay. In that case you should remove the item from the collection before binding using the Html.DropDownList. –  saravanan May 25 at 18:16
1  
Suppose you have a collection of items [duplicate primary key removed] from the controller, you can build a SelectList with the collection and then set it in the ViewData / ViewBag and then you can call Html.DropDownList with the name of the ViewData or the ViewBag. @Html.DropDownList("SelectedFonction", (IEnumerable<SelectListItem>)ViewData["SelectedFonctions"]) –  saravanan May 25 at 18:27
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.