开发者

model binding how collect data from <div>

I have model

public class MyModel { public string Name { g开发者_如何转开发et; 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)


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');
        }

    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜