我试图从客户端发布到服务(Wcf)首先我收到模型的集合我将它们转换为我的服务需要的类型,我分配并传递我想要的属性上的数据发送到服务,但我保持这个错误
对象引用未设置为对象的实例。描述:一个 在执行当前Web期间发生了未处理的异常 请求。请查看堆栈跟踪以获取有关的更多信息 错误以及它在代码中的起源。
异常详细信息:System.NullReferenceException:不是对象引用 设置为对象的实例。
有人可以帮助我,我真的很感激。
@using(Html.BeginForm("GetMarks","Questionnaire")){
for(int i = 0;i < Model.Count;i++){
<text><h3>@Model[i].QuestionAsk</h3><br /></text>
var ansList = Model[i].PossibleAnswer;
string[] singleAnswerCollection = ansList.Split(',');
@Html.HiddenFor(M => M[i].QuestionId);
foreach (var singleAns in singleAnswerCollection) {
<text>@singleAns</text>
@Html.RadioButtonFor(M =>M[i].UserAnsResponse, singleAns)
<br />
}
<br />
}
<input type="submit" name="submit" />
}
控制器
public ViewResult GetMarks(List<QuestionnaireViewModel> _AnswwerQuestionaire) {
List<Questionnaire> serviceQuestionList = new List<Questionnaire>();
serviceQuestionList = null;
foreach(var questionaire in _AnswwerQuestionaire){
serviceQuestionList.Add(new Questionnaire() { QuestionId=questionaire.QuestionId, UserAnsResponse=questionaire.UserAnsResponse});
}
var IQservice = new IQuestionService.QuestionServiceClient().GetMarks(serviceQuestionList);
return View();
服务
namespace QuestionServiceWcf.Interfaces
{
[ServiceContract]
public interface IQuestionService
{
[OperationContract]
List<Questionnaire> GetQuestionaire();
[OperationContract]
double GetMarks(List<Questionnaire> qList);
服务课 使用System.Collections.Generic; 使用System.Runtime.Serialization;
namespace QuestionServiceWcf
{
[DataContract]
public class Questionnaire
{
public Questionnaire() {
}
public Questionnaire(int _questionId) {
QuestionId = _questionId;
PossibleAnswer = new QuestionRepository().GetAnswerOptions(_questionId);
}
[DataMember]
public int QuestionId { get; set; }
[DataMember]
public string QuestionAsk { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string UserAnsResponse { get; set; }
[DataMember]
public string PossibleAnswer { get; set; }
[DataMember]
public int QuestionnaireType { get; set; }
[DataMember]
public bool isCorrectAnswer { get; set; }
}
}
客户端的类模型(MVC)
using System.Collections.Generic;
namespace PairingTest.Web.Models
{
public class QuestionnaireViewModel
{
public QuestionnaireViewModel() {
}
public QuestionnaireViewModel(int _questionId) {
QuestionId = _questionId;
}
public int QuestionId { get; set; }
public string QuestionAsk { get; set; }
public string Title { get; set; }
public string UserAnsResponse { get; set; }
public string PossibleAnswer { get; set; }
public int QuestionnaireType { get; set; }
public bool isCorrectAnswer { get; set; }
public IList<string> Questions { get; set; }
}
}