i Viewmodel Ansicht gemacht, mich zu erlauben das 2-Modell (Fax & Faxdata) in meiner Ansicht zu verwenden, aber ich habe NULL-Wert von SelectedEmployees in listboxfor was das Problem in meinem Viewmodel ??ich will Fax Tisch und FaxData Tabelle aktualisieren, die zwischen Fax und Mitarbeitern Tabellen verknüpfen
namespace FaxProject.ViewModel
{
public class SendFaxVm
{
public List<SelectListItem> Employees { set; get; }
public int[] SelectedEmployees { set; get; }
public Fax fax { set; get; }
}
}
dies ist die Ansicht (Edite Ansicht)
@model FaxProject.ViewModel.SendFaxVm
@using (Html.BeginForm("Edit","Fax"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Fax</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.fax.Id)
<div class="form-group">
@Html.LabelFor(model => model.fax.Courier_Num, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fax.Courier_Num, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fax.Courier_Num, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.fax.NumberOfPages, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fax.NumberOfPages, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fax.NumberOfPages, "",
<div class="form-group">
@Html.LabelFor(model => model.Employees, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(a => a.SelectedEmployees, Model.Employees)
@Html.ValidationMessageFor(a => a.SelectedEmployees)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Back" class="btn btn-default" />
</div>
</div>
</div>
}
i null Ausnahme im Listenfeld wurde so, wie kann ich die Liste der Mitarbeiter bearbeitet es bekommen ??
public ActionResult Edit (int id,SendFaxVm vm,Fax fax)
{
var faxindb = Db.Faxes.Single(f=>f.Id==id);
faxindb.Courier_Num = fax.Courier_Num;
faxindb.NumberOfPages = fax.NumberOfPages;
faxindb.CompanyName = fax.CompanyName;
faxindb.ReceivingDate = fax.ReceivingDate;
faxindb.Attachment = fax.Attachment;
faxindb.Subject = fax.Subject;
Db.SaveChanges();
foreach (var userId in vm.SelectedEmployees)
{
var faxdataindb = Db.FaxDatas.Single(f => f.FaxId == fax.Id);
faxdataindb.EmpId = userId;
//to do : Save fd
Db.SaveChanges();
}
return RedirectToAction("FaxForm","Fax");
}
dies die Aktion in Fax-Controller ...
Geben Sie den Controller-Code ein, um zu sehen, welche Werte verknüpft werden. Dadurch erhalten Sie weitere Informationen darüber, warum die Werte null sind. – ankur
Ich habe den Beitrag aktualisiert und hinzugefügt. –
Der eingefügte Code ist der Post-Aktion im Fax-Controller. Es sollte eine get-Methode geben, die die Werte erhält, die Sie zum Auffüllen der Listenansicht verwenden. Fügen Sie diesen Teil des Codes auch ein – ankur