gpt4 book ai didi

c# - 在部分 View 中存储 session

转载 作者:行者123 更新时间:2023-11-30 15:14:42 24 4
gpt4 key购买 nike

我正在尝试编写一个 ASP.NET MVC 应用程序,它将一个人与其他家庭成员的关系联系起来。

我正在尝试实现以下屏幕:

enter image description here

每个成员都是通过引导选项卡实现的,到目前为止,我能够根据动态生成的 ID 在选项卡之间导航。

查看标记:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

@using HouseHolderInfoSystem.Entity.Models

<script type="text/javascript">
$(function() {

// Navigation click handler
$(".relation_link").click(function(event) {

//Get the ID
const memberId = $(this).attr("data-memberId");

// AJAX call to get the partial view content
$.ajax({
url: '@Url.RouteUrl(routeName: "DisplayRelationTagger")',
type: 'POST',
dataType: 'HTML',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({
MemberId: memberId,
ApplicationId: '@ViewBag.ApplicationId'
}),
success: function(data) {
//populate the tab content.
$(`#${memberId}`).html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Could not update the div because of some error!');
}
});
});
});
</script>

<br />

<div class="container">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h2>Household Relationships</h2>
<br>
<h4>
Please tell us how the members of your household are related to each other
</h4>

<hr>
<br>

@if (ViewBag.FamilyMembers != null)
{
<ul class="nav nav-tabs">
@for (int i = 0, familyMembersCount = (ViewBag.FamilyMembers as List<FamilyMember>).Count; i < familyMembersCount; i++)
{
if (i == 0)
{
<li class="active">
<a data-toggle="tab" class="relation_link" data-memberId="@Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FamilyMemberId))" href="#@Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FamilyMemberId))"> @Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FirstName)) @Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).LastName)) </a>
</li>
}
else
{
<li>
<a data-toggle="tab" class="relation_link" data-memberId="@Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FamilyMemberId))" href="#@Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FamilyMemberId))"> @Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FirstName)) @Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).LastName)) </a>
</li>
}
}
</ul>

<div class="tab-content">
@for (int i = 0, familyMembersCount = (ViewBag.FamilyMembers as List<FamilyMember>).Count; i < familyMembersCount; i++)
{
if (i == 0)
{
<div id="@Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FamilyMemberId))" class="tab-pane fade in active">
@(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FirstName)) @Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).LastName))
</div>
}
else
{
<div id="@Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FamilyMemberId))" class="tab-pane fade">
@(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).FirstName)) @Html.Raw(Html.AttributeEncode((ViewBag.FamilyMembers[i] as FamilyMember).LastName))
</div>
}
}
</div>
}
<br>
</div>
</div>

<div class="row">
<div class="col-lg-4">
@Html.RouteLink("Back", "", null, new { @class = "btn btn-lg btn-danger" })
</div>
<div class="col-lg-4">
</div>
<div class="col-lg-4">
@Html.RouteLink("Save and submit", "", null, new { @class = "btn btn-lg btn-primary" })
</div>
</div>
</div>

部分 View 代码:

@using HouseHolderInfoSystem.Entity.Models

<script>
function ChangeRelationSelection(CurrentMemberId, RelatedFamilyMemberId, Relation ) {
if ( !CurrentMemberId || !RelatedFamilyMemberId || !Relation )
{
alert( `Current member ID or Related member ID or Relation cannot be empty!` );
return;
}


const url = `@Url.RouteUrl(routeName: "UpdateRelationTagging")`;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify( { FamilyMemberId: CurrentMemberId, RelatedFamilyMemberId: RelatedFamilyMemberId, Relation: Relation } ),
};

fetch( url, options )
.then(res => res.json())
.then( data => console.log(data.message))
.catch(error => console.error( error.message ));
}



</script>

@if (ViewBag.CurrentMember != null && ViewBag.RemainingMembers != null)
{
<br>
<fieldset>
<legend>Relationships of @Html.Raw(Html.AttributeEncode((ViewBag.CurrentMember as FamilyMember).FirstName)) @Html.Raw(Html.AttributeEncode((ViewBag.CurrentMember as FamilyMember).LastName))</legend>


@foreach (FamilyMember familyMember in (List<FamilyMember>)ViewBag.RemainingMembers)
{
<div style="display:block" class="form-group">
<span style="color:red; font-size:16px;">* </span>
<span>
Relationship to @(Html.AttributeEncode((familyMember as FamilyMember).FirstName)) @Html.Raw(Html.AttributeEncode((familyMember as FamilyMember).LastName))
</span>
<select id='@(Html.AttributeEncode(familyMember.FamilyMemberId))' class="form-control relationSelect" onchange="ChangeRelationSelection('@(Html.AttributeEncode(ViewBag.CurrentMemberId))', '@(Html.AttributeEncode(familyMember.FamilyMemberId))', event.currentTarget.value)">
<option selected disabled>----Please select a relation----</option>
<option value="Father">Father</option>
<option value="Mother">Mother</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Husband">Husband</option>
<option value="Wife">Wife</option>
</select>
</div>
<br>
}
</fieldset>

}

C# Controller 代码:

[Route("tag-relations/{ApplicationId:guid}", Name = "TagRelationGet")]
public ActionResult TagRelations(string ApplicationId)
{
Application currentApplication = _applicationService.GetApplicationById(ApplicationId);
ViewBag.FamilyMembers = currentApplication.FamilyMembersApplication.ToList();
ViewBag.ApplicationId = ApplicationId;
return View();
}

[HttpPost]
[Route("~/display-relations", Name = "DisplayRelationTagger")]
public PartialViewResult DisplayRelationTagging(RelationTaggingDTO TaggingDTO)
{
Application currentApplication = _applicationService.GetApplicationById(TaggingDTO.ApplicationId);

FamilyMember currentlySelectedFamilyMember = currentApplication.FamilyMembersApplication.FirstOrDefault(x => x.FamilyMemberId == TaggingDTO.FamilyMemberId);
List<FamilyMember> remainingFamilyMembers = currentApplication.FamilyMembersApplication.Where(x => x.FamilyMemberId != TaggingDTO.FamilyMemberId).ToList();

// Store family member member count into session for later use
Session["FamilyMemberCount"] = currentApplication.FamilyMembersApplication.ToList().Count;

ViewBag.CurrentApplicationId = TaggingDTO.ApplicationId;
ViewBag.CurrentMemberId = TaggingDTO.FamilyMemberId;
ViewBag.CurrentMember = currentlySelectedFamilyMember;
ViewBag.RemainingMembers = remainingFamilyMembers;

if (Session["RelationTagging"] != null)
{
List<UpdateRelationTaggingDTO> allRelations = JsonConvert.DeserializeObject<List<UpdateRelationTaggingDTO>>(Session["RelationTagging"].ToString());

List<UpdateRelationTaggingDTO> currentlySelectedRelations = new List<UpdateRelationTaggingDTO>();

foreach (var item in allRelations)
foreach (var innerItem in remainingFamilyMembers)
if (item.RelatedFamilyMemberId == innerItem.FamilyMemberId)
currentlySelectedRelations.Add(item);

if (currentlySelectedRelations.Count > 0)
ViewBag.TaggedRelationMembers = JsonConvert.SerializeObject(currentlySelectedRelations);
}

return PartialView("DisplayRelationshipTagger");
}

我知道我必须部分查看选项卡内容并使用 ajax 调用它,但我的问题有两个方面。

  1. 假设当我单击包含人员 1 的选项卡 1 时,我需要在表单中显示除当前人员(人员 2 和人员 3)之外的所有成员。

    enter image description here

  2. 如何将关系存储在session中,以便在完成所有关系标记后只提交一次到数据库。

最佳答案

你可以在代码中做的是在你的主页中使用Jquery

如果这是您当前的 Select 语句

<select id='@(Html.AttributeEncode(familyMember.FamilyMemberId))' class="form-control relationSelect" onchange="ChangeRelationSelection('@(Html.AttributeEncode(ViewBag.CurrentMemberId))', '@(Html.AttributeEncode(familyMember.FamilyMemberId))', event.currentTarget.value)">
<option selected disabled>----Please select a relation----</option>
<option value="Father">Father</option>
<option value="Mother">Mother</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Husband">Husband</option>
<option value="Wife">Wife</option>
</select>

您可以在“数据”属性中添加更多属性,例如

<select id='@(Html.AttributeEncode(familyMember.FamilyMemberId))' **data-familyid="@(Html.AttributeEncode(familyMember.FamilyMemberId))"
data-memberid="@(Html.AttributeEncode(ViewBag.CurrentMember.Id))"**
class="form-control relationSelect" onchange="ChangeRelationSelection('@(Html.AttributeEncode(ViewBag.CurrentMemberId))', '@(Html.AttributeEncode(familyMember.FamilyMemberId))', event.currentTarget.value)">
<option selected disabled>----Please select a relation----</option>
<option value="Father">Father</option>
<option value="Mother">Mother</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Husband">Husband</option>
<option value="Wife">Wife</option>
</select>

一旦您拥有两个 ID(即下拉列表中的 CurrentMemberID 和 FamilyMemberID)

接下来你可以做的是

$(".relationSelect").change(function(){
//Here what you can do is read the data attributes
var familyid = $(this).data("familyid");
var MemberId = $(this).data("memberid");
var relation = $(this).val();
// Next step is to send it via tha ajax to server and store it in the session, as you require
$.post("@Url.Action("StoreInSession", "Controller")", {MemberID: MemberId, familyID :familyid, relation : relation}, function(resp){

});

在 Controller 的下一部分,您可以拥有一个可以以这种格式存储数据的模型

public class RelationStore 
{
public string MemberID {get;set;}
public Dictionary<string,string> RelationAndFamily {get;set;}
}

在 Controller 中

public JsonResult(string FamilyId, string memberId, string Relation )
{
List<RelationStore> I = Session("RelationStore") as List<RelationStore>;
//Make your Logic to Add to the List and then put the list back in session
}

关于c# - 在部分 View 中存储 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53988544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com