捕获参数值

时间:2012-10-11 02:58:42

标签: c# asp.net-mvc

我真的需要你对这些家伙的帮助我被困住了,不知道从哪里开始修复。所以我有这个表格,用户可以选择案例和派对。我应该保存并传递所选项目的值。我能够保存案例选择,但我在保存所选方时遇到问题。以下是有关收集数据并保存数据的代码段。

控制器:

[HttpPost]
    [ValidateInput(false)]
    public ActionResult Create(VisitViewModel viewModel, Guid[] associatedCasesSelected, Guid[] selectedParties)
    {
        if (!ModelState.IsValid)
        {
            viewModel.Time = _timeEntryHelper.Value;
            AddLookupsToViewModel(viewModel);
            return View(viewModel);
        }
        var visitEntry = Mapper.Map<VisitViewModel, VisitEntry>(viewModel);
        visitEntry.VisitDate = _timeEntryHelper.AddTimeToDate(visitEntry.VisitDate);
        visitEntry.UserId = _currentUser.UserId;
        visitEntry.OfficeId = _currentUser.OfficeId;
        try
        {
            _visitEntryService.Create(visitEntry, associatedCasesSelected, selectedParties);
            this.FlashInfo(string.Format(Message.ConfirmationMessageCreate, Resources.Entities.Visit.EntityName));
        }
        catch (RulesException ex)
        {
            ex.CopyTo(ModelState);
        }
        if (ModelState.IsValid)
            return RedirectToAction("Edit", "Case", new { caseId = viewModel.CaseId });
        AddLookupsToViewModel(viewModel);
        return View(viewModel);
    }

VisitEntryService:

public void Create(VisitEntry visitEntry,IList<Guid>caseIds, IList<Guid>partyIds )
    {
        EnsureValid(visitEntry);
        _visitEntryRepository.Save(visitEntry);
        caseIds = AddCurrentCaseToCases(visitEntry.CaseId, caseIds,partyIds);
        foreach (var caseId in caseIds.Distinct())
        {
            var visit = new Visit {CaseId = caseId, VisitEntryId = visitEntry.VisitEntryId};
            _visitService.Create(visit);
        }
    }

VisitEntryRepository:

 public void Save(VisitEntry visitEntry)
    {
        if (visitEntry.VisitEntryId == Guid.Empty)
        {
            visitEntry.VisitEntryId = Guid.NewGuid();
            visitEntry.DateCreated = DateTime.Now;
            DataContext.VisitEntries.InsertOnSubmit(visitEntry);
        }
        else
        {
            var currentVisitEntry = Get(visitEntry.VisitEntryId);
            if (currentVisitEntry == null) throw RepositoryExceptionFactory.Create("VisitEntry", "VisitEntryId");
            currentVisitEntry.DateModified = DateTime.Now;
            currentVisitEntry.VisitDate = visitEntry.VisitDate;
            currentVisitEntry.VisitType =
                DataContext.VisitTypes.SingleOrDefault(vt => vt.VisitTypeId == visitEntry.VisitTypeId);
            currentVisitEntry.Note = visitEntry.Note;
        }
        DataContext.SubmitChanges();
    }

我不知道如何保存所选方以保存案例信息和所选案例。感谢您的任何反馈!

1 个答案:

答案 0 :(得分:0)

保存电话稍早一些,所以你的火灾后发生的变更提交更改,将SubmitChanges移动到最后你应该好好相信

<强>更新

我的意思是更改代码,如下所示,看看是否有帮助

<强>控制器:

[HttpPost]
[ValidateInput(false)]
public ActionResult Create(VisitViewModel viewModel, Guid[] associatedCasesSelected, Guid[] selectedParties)
{
    if (!ModelState.IsValid)
    {
        viewModel.Time = _timeEntryHelper.Value;
        AddLookupsToViewModel(viewModel);
        return View(viewModel);
    }
    var visitEntry = Mapper.Map<VisitViewModel, VisitEntry>(viewModel);
    visitEntry.VisitDate = _timeEntryHelper.AddTimeToDate(visitEntry.VisitDate);
    visitEntry.UserId = _currentUser.UserId;
    visitEntry.OfficeId = _currentUser.OfficeId;
    try
    {
        _visitEntryService.Create(visitEntry, associatedCasesSelected, selectedParties);
        this.FlashInfo(string.Format(Message.ConfirmationMessageCreate, Resources.Entities.Visit.EntityName));
        DataContext.SubmitChanges();
    }
    catch (RulesException ex)
    {
        ex.CopyTo(ModelState);
    }
    if (ModelState.IsValid)
        return RedirectToAction("Edit", "Case", new { caseId = viewModel.CaseId });
    AddLookupsToViewModel(viewModel);
    return View(viewModel);
}

<强> VisitEntryRepository:

public void Save(VisitEntry visitEntry)
{
    if (visitEntry.VisitEntryId == Guid.Empty)
    {
        visitEntry.VisitEntryId = Guid.NewGuid();
        visitEntry.DateCreated = DateTime.Now;
        DataContext.VisitEntries.InsertOnSubmit(visitEntry);
    }
    else
    {
        var currentVisitEntry = Get(visitEntry.VisitEntryId);
        if (currentVisitEntry == null) throw RepositoryExceptionFactory.Create("VisitEntry", "VisitEntryId");
        currentVisitEntry.DateModified = DateTime.Now;
        currentVisitEntry.VisitDate = visitEntry.VisitDate;
        currentVisitEntry.VisitType =
            DataContext.VisitTypes.SingleOrDefault(vt => vt.VisitTypeId == visitEntry.VisitTypeId);
        currentVisitEntry.Note = visitEntry.Note;
    }
}