我对 WPF 相当新手。我的理解是模型中的数据发生变化,它应该通知视图模型,并且视图将绑定到视图模型中的属性和类似的东西。它是否正确?如果是这样,我一直在读该模型应该实现INotifyPropertyChanged,并且看起来像这样
public class LoginModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public bool Authenticated { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的 ViewModel 中,我有一个属性“AuthResult”,它应该从模型属性“Authenticated”获取更新
public partial class view1 : UserControl, INotifyPropertyChanged{
public bool AuthResult
{
get
{
return _authVal;
}
set
{
_authVal = value;
NotifyPropertyChanged("AuthResult");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
Run Code Online (Sandbox Code Playgroud)
我知道当前的实施是不正确的。我发现我应该从我的模型订阅 PropertyChanged 通知,如下所示:
LoginModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(LoginModel_PropertyChanged); …Run Code Online (Sandbox Code Playgroud) 创建了一个带有名为“until”的日期时间字段的模型,名称并不重要,只是它是日期时间。这是模型规则中的验证。
['until', 'datetime', 'format' => 'Y-m-d H:i:s'],
Run Code Online (Sandbox Code Playgroud)
后来,在实例化模型并更新字段后,我收到验证错误。
$model->until = '2017-10-15 14:30:04';
$eventRepeat->validate();
$errors = $model->errors;
Run Code Online (Sandbox Code Playgroud)
$errors['until'][0] 将显示“Until 的格式无效。”
我没想到会出现这个错误。
我有以下设置。
1 个产品有多个 Product_types。许多 Product_types 有 1 种类型。根据我对文档的理解,HABTM 关系。
我的模型是
class Product < ApplicationRecord
has_and_belongs_to_many :types
end
class Type < ApplicationRecord
has_and_belongs_to_many :products
end
Run Code Online (Sandbox Code Playgroud)
我有一个这样的连接表迁移
class CreateJoinTableProductTypes < ActiveRecord::Migration[5.1]
def change
create_join_table :products, :types do |t|
t.index :product_id
t.index :type_id
end
end
end
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个表单 - 希望创建正确,现在我在表单提交上发送了以下参数:
"product"=>{"name"=>"Product A", "description"=>"A cool product", "image_dir_path"=>"./",
"type"=>{"id"=>"1"}},"commit"=>"Create Product"}
Run Code Online (Sandbox Code Playgroud)
我想知道 1)提交用于在表单和控制器中创建产品的参数的最佳/rails 约定是什么?
和
2)我如何获得插入到连接表中的记录?
我有以下获取参数的方法
def product_params
params.require(:product).permit(:name, :description, :image_dir_path, :type,)
end
Run Code Online (Sandbox Code Playgroud)
但即使这样我也可以在日志中看到 :type 的未经允许的参数
目前我的控制器只有:
@product = Product.new(product_params)
Run Code Online (Sandbox Code Playgroud)
我非常感谢任何有关创建该对象的 Rails 方式的建议。我已经阅读了 HABTM 的 api …
model.compile(
optimizer= keras.optimizers.Adam(),
loss= [keras.losses.SparseCategoricalCrossentropy(from_logits= True)
],
metrices= ['accuracy'])
model.fit(ds_train, epochs= 10, verbose=2)
Run Code Online (Sandbox Code Playgroud)
compile()类型错误: ({'metrices'},)中的关键字参数无效。有效的关键字参数包括“cloning”、“experimental_run_tf_function”、“distribute”、“target_tensors”或“sample_weight_mode”。
我正在尝试使绑定工作(遵循youtube上的教程).在这一点上,我基本上是复制粘贴代码; 它仍然没有用.
这是routes.php:
Route::model('song', 'App\Song');
Route::get('songs', 'SongsController@index');
Route::get('songs/{slug}', 'SongsController@show');
Route::get('songs/{slug}/edit', 'SongsController@edit');
Route::patch('songs/{slug}', 'SongsController@update');
Run Code Online (Sandbox Code Playgroud)
Song.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
protected $fillable = [
'title', 'lyrics'
];
}
Run Code Online (Sandbox Code Playgroud)
和SongsController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Song;
class SongsController extends Controller
{
public function index(Song $song) {
$songs = $song->get();
return view('songs.index', compact('songs'));
}
public function show(Song $song) {
return view('songs.show', compact('song'));
}
public function edit($slug) {
$song = …Run Code Online (Sandbox Code Playgroud) 如果我使用这条路线:
Route::resource('monitor', 'UserMonitorController');
Run Code Online (Sandbox Code Playgroud)
然后在控制器中:
public function update(Request $request, UserMonitor $userMonitor) {}
Run Code Online (Sandbox Code Playgroud)
由谁自动生成
php artisan make:model -mcr UserMonitor
Run Code Online (Sandbox Code Playgroud)
该$userMonitor是空的
如何foreach从IEnumerable<Model>.
代码:
public IEnumerable<Model> ListDaftarPjsp()
{
IEnumerable<Model> list = from x in db.PJSPEvaluation
select new Model
{
foo = x.Foo,
bar = x.Bar
};
foreach (Model item in list) {
item.condition = "example";
}
return list;
}
public class Model{
public string foo{ get; set; }
public string bar { get; set; }
public string condition{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了Model. 然后我使用foreach循环结果,然后设置它。但是 Return forcondition仍然没有改变?如何在foreach中设置条件然后返回结果
假设我有一个表结构表如下:
Table_A
sid (auto increment, primary)
.... some other column ....
created_at (created using laravel eloquent timestamps() function)
updated_at (same as above)
Run Code Online (Sandbox Code Playgroud)
然后,我创建了一个新列.
$new_row = new TableA; //TableA is a model created pointing to Table_A
$new_row->col1 = 'Some value';
$new_row->col2 = 'Some other value';
// Some other field
$new_row->save()
Run Code Online (Sandbox Code Playgroud)
然后,在这部分代码之后,
dd($new_row->sid);
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,输出是:
null
Run Code Online (Sandbox Code Playgroud)
但是,当我将其更改为:
dd($new_row->id);
Run Code Online (Sandbox Code Playgroud)
它返回sid(即行的主键值).当我在Laravel模型中使用自动增量作为主键时,主键的名称是否始终为id?或者为什么我在使用名称时只获得了价值id?
PS让我自己说清楚:我知道如何获得主键值,我知道我可以在模型中设置主键的名称.我想知道的是,这种行为是否适用于所有主键,或仅适用于具有主键的主键.
当我需要更新时,我会收到这个答案.我该做什么?
public function actionUpdate($id)
{
if(isset($_POST['submit']))
{
$recruit = Recruit::model()->findByPk($_POST['pid']);
$recruit->title = $_POST['title'];
$test->save();
$this->redirect(array('admin','id'=>$id));
}
$this->render('update',array(
'model'=>$this->loadModel($_POST['pid']),
'id'=>$id,
'pid'=>$_POST['pid'],
));
}
Run Code Online (Sandbox Code Playgroud) model ×9
laravel ×3
php ×3
auto ×1
binding ×1
c# ×1
controller ×1
datetime ×1
ienumerable ×1
join ×1
keras ×1
laravel-5 ×1
laravel-5.3 ×1
linq ×1
mvvm ×1
object ×1
resources ×1
routes ×1
tensorflow ×1
typeerror ×1
validation ×1
viewmodel ×1
wpf ×1
yii ×1
yii2 ×1