传递到字典中的模型项的类型为'System.Data.Linq.DataQuery`1 [SantramNetra.Models.DoctorMaster]',但是此字典需要模型项的类型... [英] The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[SantramNetra.Models.DoctorMaster]', but this dictionary requires a model item of type...

查看:102
本文介绍了传递到字典中的模型项的类型为'System.Data.Linq.DataQuery`1 [SantramNetra.Models.DoctorMaster]',但是此字典需要模型项的类型...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的看法


This is my view


<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>DoctorMaster</legend>

        <%: Html.HiddenFor(model => model.nCode) %>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cName) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cName) %>
            <%: Html.ValidationMessageFor(model => model.cName) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cHospitalName) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cHospitalName) %>
            <%: Html.ValidationMessageFor(model => model.cHospitalName) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cAddress) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cAddress) %>
            <%: Html.ValidationMessageFor(model => model.cAddress) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.nDrType) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.nDrType) %>
            <%: Html.ValidationMessageFor(model => model.nDrType) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cPhno) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cPhno) %>
            <%: Html.ValidationMessageFor(model => model.cPhno) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cMobile) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cMobile) %>
            <%: Html.ValidationMessageFor(model => model.cMobile) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cRegNo) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cRegNo) %>
            <%: Html.ValidationMessageFor(model => model.cRegNo) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.cEmail) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.cEmail) %>
            <%: Html.ValidationMessageFor(model => model.cEmail) %>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>







这是我的编辑操作方法








this is my action method for edit


public ActionResult Edit(int id)
       {
           var DocMstrEdit = from v in context.DoctorMasters where v.nCode == id select v;
           return View(DocMstrEdit);
       }



当我调用edit时会引发此错误,请帮助我






when i invoke edit it throws this error please help me




Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[SantramNetra.Models.DoctorMaster]', but this dictionary requires a model item of type 'SantramNetra.Models.DoctorMaster'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[SantramNetra.Models.DoctorMaster]', but this dictionary requires a model item of type 'SantramNetra.Models.DoctorMaster'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

推荐答案

结果归为一种模型,但您的linq查询未返回一种模型.该问题将被var关键字吞噬.如果您将其更改为键入Models.DoctorMaster,我想您会更早看到该错误.要解决此问题,请将您的linq查询更改为如下所示:

It sounds like you are trying to put the results into one model but your linq query isn''t returning one model. That issue will be eaten up by the var keyword. If you were to change that to be type Models.DoctorMaster instead, I think you would see the error earlier. To fix this, change your linq query to look like this:

public ActionResult Edit(int id)
{
   Models.DoctorMaster DocMstrEdit = (from v in context.DoctorMasters 
                                      where v.nCode == id 
                                      select v).FirstOrDefault();
   return View(DocMstrEdit);
}



这将确保您仅将一个模型返回到变量中.当您期望一个模型时,即使您查询的值仅应返回一个条目,也总是最好这样做.



That will ensure that you only return one model into your variable. When you are expecting one model, it is always best to do this, even if you are querying a value that should only return one entry.


这篇关于传递到字典中的模型项的类型为'System.Data.Linq.DataQuery`1 [SantramNetra.Models.DoctorMaster]',但是此字典需要模型项的类型...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆