你能将模型属性从asp.net模型传递到reactjs.net组件吗?

时间:2017-01-03 21:22:44

标签: asp.net asp.net-mvc reactjs asp.net-core reactjs.net

我正在使用最新的asp.net核心与reactjs.net并渲染我的反应组件服务器端,我是否有可能不仅传递模型,而且传递模型属性以进行验证,而无需设置依赖关系对于JS验证器?

模型

public class LoginViewModel
    {
        [Required]
        [EmailAddress]
        [StringLength(50)]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }

查看

@model LoginViewModel
@{
    Layout = "~/Views/Shared/_LoginLayout.cshtml";
}
@Html.React("LoginPage", new { loginData = Model })    
@Html.ReactInitJavaScript()

如果没有,那么验证模型的最有效方法是什么,而无需在视图上重新定义整个模型

1 个答案:

答案 0 :(得分:0)

您应该自行管理验证,因为React具有不同类型的验证调用PropType。例子

import React from "react";
import PropTypes from "prop-types";

import moment from "moment/src/moment";
import PostShare from "./PostShare.jsx";

const PostList = props => {
    const categories = JSON.parse(props.post.categories) || [];
    const tags = JSON.parse(props.post.tags) || [];
    const imageSrc = props.medias ? props.medias.path : "./images/No_Image_Available.jpg";

    return (
        <div className="main-post-area-holder">
            <article className="post-details-holder layout-two-post-details-holder wow  fadeInUp">
                <div className="row">
                    <div className="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                        <div className="post-image">
                            <img src={imageSrc} alt="...." />
                        </div>
                    </div>
                    <div className="col-lg-7 col-md-7 col-sm-7 col-xs-12"> 
                        <div className="post-meta-category">
                            <ul className="tags">
                                {categories.map((categorie, index) => {
                                    return (
                                        <li key={index}>
                                            <a href="#" className="tag">
                                                {categorie}
                                            </a>
                                        </li>
                                    );
                                })}
                                {tags.map((tag, index) => {
                                    return (
                                        <li key={index}>
                                            <a href="#" className="tag">
                                                {tag}
                                            </a>
                                        </li>
                                    );
                                })}
                            </ul>
                        </div>
                        <div className="post-title">
                            <h2>
                                <a href="#">
                                    {props.post.title}
                                </a>
                            </h2>
                        </div>
                        <div className="post-meta-posted-date">
                            <p>
                                <a href="#">
                                    {moment(props.post.dateCreated).format("DD MMMM YYYY")}
                                </a>
                            </p>
                        </div>

                        <PostShare />
                    </div>
                </div>
            </article>
        </div>
    );
};

PostList.propTypes = {
    post: PropTypes.object,
    medias: PropTypes.object
};

export default PostList;