0

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;
1
  • 1
    Do you have any property in the FlowViewModel for the value of the text area to bind to. Commented May 25, 2013 at 17:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

thx,,,yes I already did that and it' s works but not witth collection but with the request like this : var value1 = Request["S1"];
the problem now that the action didn't works (not saving in the base) when the script is functionnal
what exception are you getting.
Okay. In that case you should remove the item from the collection before binding using the Html.DropDownList.
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"])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.