views:

17

answers:

1

I have model

public class MyModel { public string Name { get; set;} }

I have html code

<div id="Name">Important data</div>

In Controller, i have a method

public ActionResult Index (MyModel model)
{
    model.Name == "Important data"
}

Is it real, using standart model binder? thx)

+4  A: 

The model binder parses request data into objects. When you use a div there's no request data, nothing posted to the server. You could use a form with input fields in order to send request parameters that could be handled by the model binder and converted into object:

<% using (Html.BeginForm()) { %>
    <div id="name"><%= Html.TextBoxFor(x => x.Name) %></div>
    <input type="submit" value="OK" />
<% } %>

Or use an anchor:

<%= Html.ActionLink("send to server", "index", new { Name = "some name to send" }) %>

Another option would be to send the request using AJAX:

$(function() {
    $.ajax({
        url: '/home/index',
        data: { name: $('#name').html() },
        success: function(result) {
            alert('success');
        }

    });
});
Darin Dimitrov
or, if you just want the name displayed but not editable, include a hidden input field within your DIV that contains the value of "Name".
dave thieben