当前位置:编程学习 > C#/ASP.NET >>

关于MVC UpdateModel()方法

public ActionResult Edit(int id, FormCollection form)
{
     var model = music.SingerInfo.First(c => c.SingerID == id);
     UpdateModel(model, new[] {"SingerName","Area" });
     music.SaveChanges();
     return RedirectToAction("Index");
}

一个更新页面,model获得实体模型,然后new[] {"SingerName","Area" }这里,我只传过去一个id,为什么更新的时候会根据id查询到SingerName和Area列的值?还有,这个id是routes里定义的那个id吧? --------------------编程问答-------------------- var model = music.SingerInfo.First(c => c.SingerID == id);
根据id查询到c.SingerID == id的music实例(自然包括了你要更新的SingerName和Area属性) --------------------编程问答--------------------
引用 1 楼 dongxinxi 的回复:
var model = music.SingerInfo.First(c => c.SingerID == id);
根据id查询到c.SingerID == id的music实例(自然包括了你要更新的SingerName和Area属性)


我可能没说仔细,这是张edit页面,我在前台修改好信息后,提交,跳到这段代码,而查询只是查询到没修改之前的那条数据,通过updatemodel()后才更新成新的数据,然后再save,所以我很纳闷,updatemodel里的"SingerName","Area"2个数据列的数据是如何得到的 --------------------编程问答--------------------

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC.Models.SingerInfo>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        
        <fieldset>
            <legend>Fields</legend>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.SingerID) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.SingerID) %>
                <%: Html.ValidationMessageFor(model => model.SingerID) %>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.SingerName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.SingerName) %>
                <%: Html.ValidationMessageFor(model => model.SingerName) %>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Area) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Area) %>
                <%: Html.ValidationMessageFor(model => model.Area) %>
            </div>
            
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>


[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection form)
        {
            var model = music.SingerInfo.First(c => c.SingerID == id);
            UpdateModel(model, new[] {"SingerName","Area" });
            music.SaveChanges();
            return RedirectToAction("Index");
        }
--------------------编程问答-------------------- 怎么没人呢?
--------------------编程问答-------------------- 你可以像ASP那样通过request进行获取
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,