Pass parameter to server with knockoutmvc

46 views
Skip to first unread message

Ramón Lerena

unread,
Jun 22, 2017, 7:03:09 AM6/22/17
to Knockout MVC

Hello everybody!!

I've been asked to build something with knockoutmvc, but as I don't have experience with Javascript, I am a little lost.
I have searched a lot but I have not been able to find a (simple) solution.

Here is an example of the problem I'm facing:


This is the Model:

        public class Entity
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public List<Elements> Status{ get; set; }
        }

        public class Elements
        {
            public string Id { get; set; }
            public string Type { get; set; }
        }


View, 

        @using PerpetuumSoft.Knockout
        @model  WebApplication.Models.Entity
        @{
            ViewBag.Title = "Detail";
            var ko = Html.CreateKnockoutContext();
        }

        <div>
            <label class="col-sm-2 control-label">Name</label>
            @ko.Html.TextBox(m => m.Name, new { @class = "form-control" })
            
            <span>Colegas:</span>
            @using (var status= ko.Foreach(m => m.Status))
            {
                <ul>
                    <li>
                        @status.Html.TextBox(m => m.Type, new { @class = "form-control" })
                        @ko.Html.Button("Reset Type", "ResetType", "Detail", new { id = @status.Bind.Value(m => m.Id) })
                    </li>
                </ul>
            }
        </div>

        @ko.Apply(Model)


And Controller:

        public ActionResult AddMote(Entity model, string id)
        {
            //I don't know how to receive the id value inside that foreach... :(
        }

The problem I have is that I'm not able to pass the id value when the "Reset Type" button is clicked.
I suppose that I have to implement something with Javascript, but as I said due to my experience with it, I am lost.

So I would like to kindly ask for some help or guidance on how to follow...
Thanks in advance!!!

Ramón Lerena

unread,
Jun 22, 2017, 7:07:37 AM6/22/17
to Knockout MVC
The Controller has a (copy/paste) error, the method name would be ResetType, not AddMote:


        public ActionResult ResetType(Entity model, string id)
Reply all
Reply to author
Forward
0 new messages