C#Razor:如何在视图中更新模型属性值?

时间:2018-07-23 19:01:37

标签: c# razor

当我切换按钮时,如何更新绑定在视图中的模型属性值?是否可以在Razor中完成此操作而无需使用JQuery / Javascript?我正在尝试有条件地显示/隐藏div(验证消息),并在显示div时绑定模型属性值(即,如果StoreExists === false)。目前,它仅在页面加载时绑定。这是我的以下代码:

控制器:

public JsonResult GetStoreSettings(Form form)
        {
            string storeNumber= form.StoreNumber;
            bool isStoreFranchise= form.IsStoreFranchise
            Model.SetStoreType(storeNumber, isStoreFranchise);
            return Json(Model);
        }

型号:

public string StoreType { get; set; }
public bool StoreExists { get; set; }

public void SetStoreType(string storeNumber, bool isFranchiseSelected)
        {     
            if (storeNumber == String.Empty &&  isFranchiseSelected)
            {
                StoreType = "franchise";
            }
            else if (storeNumber == String.Empty && !isFranchiseSelected)
            {
                StoreType = "corporate";
            }
            else
            {
                StoreExists = true;
            }            
        }

查看:

@if (!Model.StoreExists)
            {
                <div>@($"There is no {Model.StoreType} store for this state")</div>
            }

0 个答案:

没有答案