开发者

ASP.NET MVC DataViewModel Problem no insert and edit

using the code DataViewModel with one form for create and edit with partial view , in the code below In the createI am not able to enter the values to the database,In the edit Mode I am not able to display the value as well in the textboxes for edit(The record is created but no form value is going to the database).

 public  class OrganizationGroupFormViewModel
{

// Properties
    public OrganizationGroup OrganizationGroup { get; private set; }

    public OrganizationGroupFormViewModel(OrganizationGroup organizationGroup)
    {
        OrganizationGroup = organizationGroup;

    }

}

public class OrganizationGroupsController : Controller
{
    //
    // GET: /OrganizationGroups/
    OrganizationGroupsRepository OrganizationGroupRepository = new OrganizationGroupsRepository();
    OrganizationUsersDataContext _db = new OrganizationUsersDataContext();

    public ActionResult Create()
    {
        try
        {
            OrganizationGroup OrgGroup = new OrganizationGroup()  {
                int_CreatedBy=1,
                dtm_CreatedDate=DateTime.Now

            };

            return View(new OrganizationGroupFormViewModel(OrgGroup));
        }
        catch
        {
            return View();
        }

          }
开发者_如何学运维

    [HttpPost]
    public ActionResult Create(OrganizationGroup OrgGroup)
    {


        if (ModelState.IsValid)
        {


            OrgGroup.int_CreatedBy = 1;
            OrgGroup.dtm_CreatedDate = DateTime.Now;
            OrganizationGroupRepository.Add(OrgGroup);
            OrganizationGroupRepository.Save();

            return RedirectToAction("Details", new { id = OrganizationGroupRepository.int_OrganizationGroupId });

        }
        return View(new OrganizationGroupFormViewModel(OrgGroup));
    }

    //
    // GET: /OrganizationGroups/Edit/5

    public ActionResult Edit(int id)
    {
        try
        {
            var OrgGroup = _db.OrganizationGroups.First(m => m.int_OrganizationGroupId == id);

            if (ModelState.IsValid)
            {
                OrgGroup.int_ModifiedBy = 1;
                OrgGroup.dtm_ModifiedDate = DateTime.Now;


            }
            return View(new OrganizationGroupFormViewModel(OrgGroup));
        }
        catch
        {
            return View();
        }
    }

    //
    // POST: /OrganizationGroups/Edit/5

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {

            var OrgGroup = _db.OrganizationGroups.First(m => m.int_OrganizationGroupId == id);

            if (ModelState.IsValid)
            {
                OrgGroup.int_ModifiedBy = 1;
                OrgGroup.dtm_ModifiedDate = DateTime.Now;
                TryUpdateModel(OrgGroup);
                OrganizationGroupRepository.Save();
            }

            return RedirectToAction("Details", new { id = OrgGroup.int_OrganizationGroupId });
        }
        catch
        {
            return View();
        }
    }

Create View;

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

Create Organization Group

<h2>Create</h2>
    <%= Html.ValidationSummary(true) %>

<div>
    <% Html.RenderPartial("OrganizationGroup"); %>
</div>

Organization Group User Control

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Egovst.Controllers.OrganizationGroupFormViewModel>" %>

<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>


        <div class="editor-label">
            Organization Group Name:
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.OrganizationGroup.vcr_OrganizationGroupName)%>
            <%= Html.ValidationMessageFor(model => model.OrganizationGroup.vcr_OrganizationGroupName)%>
        </div>

        <div class="editor-label">
              Organization Group Description:
        </div>
        <div class="editor-field">
            <%= Html.TextAreaFor(model => model.OrganizationGroup.vcr_OrganizationGroupDesc)%>
            <%= Html.ValidationMessageFor(model => model.OrganizationGroup.vcr_OrganizationGroupDesc)%>
        </div>


        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>


I think it (might) be because your view is inheriting from OrganizationGroupFormViewModel and in the post of the view you are using the model OrganizationGroup.

I'd try accepting the OrganizationGroupFormViewModel in the post though this is untested.

EDIT

The other thing you might try is in your textboxes using the name that is fully qualified to your model.

<%= Html.textbox("model.field", model.field) %>

I've found this helps when I'm not passing in the form view model to my controller.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜