Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm just starting out with MVC, done a quite a bit of WebForms so not a noob.
So far MVC is fairly intuitive and I'm catching on pretty quick but I'm stuck on one critical area;

I've set up a bootstrap-tagsinput[^] control on my view page and I'm able to collect the data in a JQuery function.

I've set up a ViewModel class and can pass values into and show them on the control but I'm stuck as to how to return these values to the controller??

Here's my view code;

HTML
@model JaxCoderV20.Models.PostViewModel

@{
    ViewBag.Title = "Home Page";
}

@using (Ajax.BeginForm("Calculate", "Home",
                            new AjaxOptions { UpdateTargetId = "divInterestDeatils" }))
{
    <div class="page-body">
        <div class="row">
            <div class="col-lg-6 col-sm-6 col-xs-12">
                <div class="well with-header">
                    <div class="header bordered-darkorange">Bootstrap Tags Input</div>
                    <div>
                        <input type="text" value="@Html.Encode(Model.TestStringData) " data-role="tagsinput" placeholder="Add tags" />
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Create" class="btn btn-default" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}
@section PageScripts{
    <!--Bootstrap Tags Input-->
    <script src="/assets/js/tagsinput/bootstrap-tagsinput.js"></script>
    <script>
        $( document ).ready(function() {
            $('form').on('submit',function()
            {   
                @Model.TestStringData = $('input').tagsinput('items')
            });
        });
    </script>
}


Then in the controller I have;

C#
[HttpPost]
public ActionResult Calculate(PostViewModel model)
    {
       return Content("This is a test");
     }
}


and just for good measure here's my PostViewModel code;

SQL
namespace JaxCoderV20.Models
{
    public class PostViewModel
    {
        public Post PostData { get; set; }
        public Message MsgData { get; set; }
        public List<string> TestData { get; set; }
        public string TestStringData { get; set; }
    }
}



I have a break on the post and it happens but all fields in the PostViewControl are null.
I also put a break at the script and there is valid data there.

Thanks
Posted
Comments
Kornfeld Eliyahu Peter 8-Feb-15 4:35am    
In MVC - as opposed to WebForm - there is no such a thing 'server side' (for the controls, the only server side is a bunch of actions you can call...), so there is no automatic data transfer, but only via call of an action with the proper data...
You also should use the helper methods of the Razor engine (or any other engine of your choice) to render input elements connected to data...
Please read this article to see how to start - http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/
Mike Hankey 8-Feb-15 5:48am    
That answered all my questions thanks! I don't see a way to mark it as solved or give credit other than saying thanks.
Kornfeld Eliyahu Peter 8-Feb-15 5:49am    
Thanks will do :-)

1 solution

Hello,

write below code in controller,
C#
[HttpPost]
public ActionResult Calculate(PostViewModel model)
    {
      model.TestStringData="your value";
      return View(model);
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900