我有部分联系视图。目前索引视图显示联系人详细信息的此部分视图。部分视图内部有一个保存按钮,用于保存编辑的数据。保存编辑后的数据时会对年龄进行验证。这很好。
每当用户编辑年龄并保存时,我需要在主视图上显示相应的占星预测。我们如何实现它?
public class ContactEntity
{
public int ContactID { get; set; }
public string ContactName { get; set; }
[Range(18, 50, ErrorMessage = "Must be between 18 and 50")]
public int ContactAge { get; set; }
}
public class AgeHoroscope
{
public int Age { get; set; }
public string HoroscopePrediction { get; set; }
}
// Home Controller
namespace MYContactEditPartialViewTEST.Controllers
{
public class HomeController : Controller
{
List<AgeHoroscope> horoList = new List<AgeHoroscope>()
{
new AgeHoroscope{Age=16,HoroscopePrediction="You are confused"},
new AgeHoroscope{Age=26,HoroscopePrediction="You are very brilliant"},
new AgeHoroscope{Age=27,HoroscopePrediction="You are practical"}
};
public ActionResult Index()
{
AgeHoroscope selectedHoro = horoList[1];
return View(selectedHoro);
}
}
}
//联系控制器
namespace MYContactEditPartialViewTEST.Controllers
{
public class ContactController : Controller
{
public PartialViewResult MyContactDetailEdit()
{
Thread.Sleep(500);
return PartialView(GetContact());
}
[HttpPost]
public PartialViewResult MyContactDetailEdit(string conatcclick)
{
//Save to database
Thread.Sleep(500);
return PartialView(GetContact());
}
private ContactEntity GetContact()
{
ContactEntity contactEntity = new ContactEntity();
contactEntity.ContactID = 1;
contactEntity.ContactName = "Lijo";
contactEntity.ContactAge = 26;
return contactEntity;
}
}
}
// Index.cshtml
@model MYContactEditPartialViewTEST.AgeHoroscope
@{
ViewBag.Title = "Index";
}
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>
Index</h2>
<div>
<a>Your age is <b>@Html.DisplayFor(x => x.Age) </b>and the prediction is <b>" @Html.DisplayFor(x => x.HoroscopePrediction)
" </b></a>
<br />
</div>
<div style="border: 3px solid Teal">
@Html.Action("MyContactDetailEdit", "contact")
</div>
// MyContactDetailEdit.cshtml
@model MYContactEditPartialViewTEST.ContactEntity
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<h3>MyContactDetailEdit PARTIAL</h3>
<div>
@Html.HiddenFor(x => x.ContactID)
<br />
<div style="font-weight:bold">
Name:
<br />
</div>
@Html.DisplayFor(x => x.ContactName)
<br />
<br />
<div style="font-weight:bold">
Age
<br />
</div>
@Html.EditorFor(x => x.ContactAge)
@Html.ValidationMessageFor(model => model.ContactAge)
<br />
<br />
</div>
<input type="submit" id="saveButton" value="Save" />
}
阅读
答案 0 :(得分:1)
我想使用jQuery来执行ajax发布,然后直接更改父视图客户端
答案 1 :(得分:0)
您需要创建一个新的ViewModel才能执行此操作。这个ViewModel(IndexViewModel.cs)看起来像这样(我猜这个):
public class IndexViewModel
{
public int ContactID { get; set; }
public string ContactName { get; set; }
public int ContactAge { get; set; }
public string HoroscopePrediction { get; set; }
}
然后在控制器索引操作(和视图)中使用它:
@model MYContactEditPartialViewTEST.IndexViewModel
这个想法是你在ContactEntity和AgeHoroscope(或通过Linq等)之间的联接中填充HoroscopePrediction,从而将索引中的每一行显示为一个完整的对象(显示联系人和占星术)。
答案 2 :(得分:0)
当数据发布到“HomeController”和“Index”操作时,在View中更改年龄时会反映出更改。
尝试按如下方式修改家庭控制器,然后它将按预期工作。
1)我们可以提供年龄和预测字典,而不是拥有AgeHoroscope列表。
2)为HttpGet和HttpPost创建两个索引操作,如下所示。
public class HomeController : Controller
{
Dictionary<int, string> AgePred = new Dictionary<int, string>()
{
{16,"You are confused"},
{26,"You are very brilliant"},
{27,"You are practical"}
};
[HttpGet]
public ActionResult Index()
{
AgeHoroscope selectedHoro = new AgeHoroscope() { Age = 26 };
selectedHoro.HoroscopePrediction = AgePred[selectedHoro.Age];
return View(selectedHoro);
}
[HttpPost]
public ActionResult Index(AgeHoroscope model,ContactEntity entity)
{
model.Age = entity.ContactAge;
model.HoroscopePrediction = AgePred[entity.ContactAge];
return View(model);
}
}