Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC & JQuery Dynamic Form Content

I would like to dynamically add fields to an ASP.NET MVC form with JQuery.

Example:

<script language="javascript" type="text/javascript">
    var widgets;

    $(document).ready(function() {
        widgets = 0;
        AddWidget();
    });

    function AddWidget() {
        $('#widgets').append("<li><input type='text' name='widget" + widgets + "'/></li>");
        widgets++;
    }
</script>

<ul id="widgets">
</ul>

This works, but I was going to manually iterate the form values in the controller:

[AcceptVerbs("Post")]
public ActionResult AddWidget(FormCollection form)
{
    foreach (string s in form)
    {
        string t = form[s];
    }

    return RedirectToAction("ActionName");
}

But it occurred to me when I send the user back to the Get Action in the Controller I will have to set the FormData with the values entered and then iteratively add the widgets with <% scripting.

What is the est way to do this in the current release (5 I believe)?

like image 790
blu Avatar asked Jan 21 '09 00:01

blu


People also ask

What is ASP.NET MVC used for?

MVC is a design pattern used to decouple user-interface (view), data (model), and application logic (controller). This pattern helps to achieve separation of concerns.

Is ASP.NET MVC easy to learn?

It's really difficult to try and learn an entirely new language/framework under pressure. If you're required to deliver working software for your day job, trying to learn ASP.NET Core at the same time might be heaping too much pressure on yourself.

What is MVC in ASP.NET with example?

MVC is a pattern known as Model-View-Controller. It is made up of three pieces; Model, View, and Controller that interact with each other to provide capabilities to the application that implements it. MVC is related to ASP.NET in much the same way as it is to an application.

Is ASP.NET and ASP.NET MVC same?

ASP.NET is a web platform. It provides a layer that sits on top of IIS (the web server) which facilitates the creation of web applications and web services. ASP.NET MVC is a framework specifically for building web applications. It sits ontop of ASP.NET and uses APIs provided by ASP.NET.


1 Answers

My solution could be something like this (pseudo-code):

<script language="javascript" type="text/javascript">
    var widgets;

    $(document).ready(function() {
        widgets = 0;
        <% for each value in ViewData("WidgetValues") %>
             AddWidget(<%= value %>);
        <% next %>
    });

    function AddWidget( value ) {
        $('#widgets').append("<li><input type='text' name='widget" + widgets + 
                             "'>" + value + "</input></li>");
        widgets++;
    }
</script>

<ul id="widgets">
</ul>

And in the controller:

[AcceptVerbs("Post")]
public ActionResult AddWidget(FormCollection form)
{
    dim collValues as new Collection;
    foreach (string s in form)
    {
        string t = form[s];
        collValues.add( t )
    }
    ViewData("WidgetValues") = collValues;
    return RedirectToAction("ActionName");
}

You can work out the details later
(sorry for mixing VB with C#, I'm a VB guy)

like image 94
Eduardo Molteni Avatar answered Oct 20 '22 15:10

Eduardo Molteni