asp.net mvc DataViewModel Problem no insert and edit

Posted by mazhar on Stack Overflow See other posts from Stack Overflow or by mazhar
Published on 2010-05-16T16:34:44Z Indexed on 2010/05/16 16:40 UTC
Read the original article Hit count: 233

Filed under:
|

using the code DataViewModel with one form for create and edit with partial view , in the code below In the create*I 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

 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>

<% } %>

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc