简体   繁体   English

返回包含javascript的部分视图是不好的做法吗?

[英]Is it bad practice to return partial views that contain javascript?

The replies in this post sound very convincing, however, this post is just over my head and was hoping for some help: 这篇帖子中回复听起来很有说服力,但是,这篇帖子刚刚超出我的脑海,并希望获得一些帮助:

I have code in my partial view that looks like this: 我在部分视图中有如下代码:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<div class="messageBox1"></div>

<% Html.BeginForm("BusinessAdd", "Home", FormMethod.Post, new { id = "form1" }); %>

<label for="BusinessName">Business Name: </label>
<%= Html.TextBox("TxtBusinessName") %>
<a href="#" onclick="if ($('#form1').valid()) { $('#form1').submit(); } return false;">submit</a>

<script type="text/javascript">
    $("#form1").validate({
        errorLabelContainer: ".messageBox1",
        rules: {
            "TxtBusinessName": { required: true }
        },
        messages: {
            "TxtBusinessName": { required: "Required field" }
        }
    });
</script>
<% Html.EndForm(); %>

Thanks, rodchar 谢谢,Rodchar

PS Will this question get seen or should I have posted a new question and referenced this post? PS:这个问题会被看到吗?还是我应该发布一个新问题并引用该帖子?

I don't think there is a problem with doing what you quoted above. 我认为您在上面所做的事情没有问题。 I do it in my production mvc app . 我在生产mvc应用程序中执行此操作 Sure if the volume of js you load onto the page after a full request gets massive then look for another option but if it's a couple of partials like this then it's fine. 当然,如果在一个完整的请求之后加载到页面上的js量变大,则可以寻找另一个选择,但是如果它是这样的两个部分,那就可以了。

Be practical. 务实。 Don't over engineer. 不要过度工程师。 And try not to optimise early. 并尽早优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM