1

如果这有点长,我很抱歉,但这是我的第一篇文章,我知道我可能会被信息很少的帖子推迟,所以就这样吧。

我目前在具有强类型 ViewModel 的视图中使用 ASP.NET MVC 4 进行编码。ViewModel 很简单,由原始数据类型组成。该模型从不用于在数据库中插入或更新数据,并且仅用于一页。单击提交按钮时,我使用控制器对数据库执行各种查询,以查看某些字段是否有效(数据注释无法处理的条件),如果没有无效字段,则发送数据到我的流程的下一步。

我的情况是这样的。在不刷新页面的情况下,我需要:

  1. 检查 ContactID 是否已更改
  2. 如果 ContactID 已更改并已填充,请检查数据库中的 Contact 表以查看 ContactID 是否存在
  3. 如果数据库中存在 ContactID,则在模型中的相应文本框中显示联系人的信息并禁用这些文本框
  4. 如果 ContactID 不存在,则在 ContactID 的验证消息中显示模型错误或某种通知(我不希望发出警报,但我并不挑剔),并将模型中的每个文本框设置为 null

该标准主要针对用户。用户需要查看要提交的联系人。我无法进行列表视图,因为如果用户选择不输入 ContactID,他们仍然需要能够输入联系信息。话虽如此,我仍然会验证用户何时单击提交以防万一。

看法

@model ViewModels.OrderViewModel

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()               
@Html.ValidationSummary(true)

<table>
  <tr>
       <td>
           @Html.TextBoxFor(model => model.OtherDetails, new { id="OtherDetails" })
       </td>
  </tr>
  <tr>
       <td>
            @Html.TextBoxFor(model => model.ContactID, new { id="ContactID" })
            @Html.ValidationMessageFor(model => model.ShipTo)
       </td>
  </tr>
  <tr>
       <td>
            @Html.TextBoxFor(model => model.ContactStreet, new { id = "ContactStreet" })
       </td>
       <td>
            @Html.TextBoxFor(model => model.ContactCity, new { id = "ContactCity" })
       </td>
       <td>
            @Html.TextBoxFor(model => model.ContactState, new { id = "ContactState" })
       </td>
  </tr>

</table>
<input type="submit" value="Submit" />
}

@section scriptContent
{
    <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>*@
    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("form :input:enabled:visible:first").focus();
            $("form :input:enabled:visible:first").select();
            $(".input-validation-error").first().focus();
            $(".input-validation-error").first().select();
        });

        //THIS IS MY CURRENT CODE 
        $('#ContactID').change(function () {
            var ContactIDValue = $('#ContactID').val();

            $.ajax({
                url: '/Order/GetContactInformationForContactID/',
                type: 'POST',
                data: { ContactIDValue: ContactIDValue },
                success: function (result) { alert(result.STREET); }
                   //My database column is called STREET
            });
        });
    </script>
}

模型

public class OrderViewModel
{
    public string OtherDetails { get; set; }
    public string ContactID { get; set; }
    public string ContactStreet { get; set; }
    public string ContactCity { get; set; }
    public string ContactState { get; set; }

}

控制器

[HttpGet]
public ActionResult Index()
{
    //private :)
}

[HttpPost]
public ActionResult Index(OrderViewModel)
{
    //private :)
}

[HttpPost]
public ActionResult GetContactInformationForContactID(string ContactIDValue )
{
    var result = myDB.CONTACTs.Where(r => r.CONTACTID == ContactIDValue ).FirstOrDefault();

    return Json(result );
}

我的代码现在的工作方式是当用户输入 ContactID 时,页面会返回警报。如果匹配,警报将显示来自数据库的 STREET 值。如果没有匹配,只会有一个空白的警报框。

我尝试给每个文本框一个 id,然后使用 javascript 调用它们,然后用 Json 更新它们,但我没有成功。我如何尝试如下所示。也许我做错了?

$('#ContactStreet').Val = result.STREET;

我试过弄乱一些局部视图,但我必须承认我对它们并不精通,我不确定模型的完整性是否会得到保留。如果可能,我想始终使用相同的模型,但我对其他解决方案持开放态度。如果我最终需要两个单独的模型,当用户单击提交按钮时,我可能会同时需要它们。

我期待一些回应;这是过去一周消耗我时间的一个问题。

4

1 回答 1

0

Try $('#Street').Val(result.STREET);

about Jquery .val()

于 2013-11-07T21:08:35.020 回答